简体   繁体   English

将数组从JavaScript传递到Wordpress中的页面

[英]Pass array from javascript to page in wordpress

I have a page in wordpress, that uses an jquery and ajax to get information from an external api. 我在wordpress中有一个页面,该页面使用jquery和ajax从外部api获取信息。 The form sends the array generated in javascript back to the same page with another variable that the php in the page uses to determine which page to display. 表单将javascript中生成的数组与另一个变量一起发送到同一页面,该页面中的php使用该变量来确定要显示哪个页面。 Outside of wordpress, the code works fine. 在wordpress之外,代码可以正常工作。 Inside Wordpress the first portion runs, but then instead of loading the same page again it goes to a search page and says nothing found. 在Wordpress中,第一部分运行,但是随后没有再次加载同一页面,而是转到搜索页面,什么都没找到。

The url on the output is: 输出的网址是:

http://kltools.net/?s=&post_type%5B%5D=portfolio&post_type%5B%5D=post&post_type%5B%5D=page

Which seems odd considering I'm using post not get. 考虑到我正在使用post not get,这似乎很奇怪。

The javascript that generates the array and submits the form: 生成数组并提交表单的javascript:

function submitchans(){
  for (var i=0;i<chans.length;i++)
  { var newHidInp = document.createElement('input');
        newHidInp.type  = 'hidden';
        newHidInp.name  = 'chans[]';
        newHidInp.value = chans[i];
    form.appendChild(newHidInp);
  }
}
function livearray(input){
    if (input != null) {
    chans.push(input);
    }
    if (Y === cSize){
        submitchans();
        document.forms[0].submit();
    }

}

Previously the array was outArray[] instead of chans[], I changed it thinking that may be triggering the result, but no luck. 以前,数组是outArray []而不是chans [],我更改了它以为可能触发结果,但是没有运气。

This is the PHP portion of the code: 这是代码的PHP部分:

<?php
$page_to_load = $_POST[view];
switch($page_to_load) {
    case '':
        echo "<script src=\"../scripts/jquery-3.2.0.min.js\"></script>";
        echo "<script type=\"application/javascript\" src=\"../scripts/raid.js\"></script>";
        echo "<font size=\"+3\" color=\"#FFFFFF\">Who should I host?<br>Please wait while channel is selected<br></font>";
        echo "<font size=\"+2\">";
        echo "<br><br>";
        echo "<img src=\"../_images/ajax_loader_blue_350.gif\">";
        $servername = "localhost";
        $username = "username";
        $password = "password";
        $dbname = "database";
        $chanarray[] = null;
        $offline = 0;

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT `TwitchNames` FROM TK_Members WHERE Validated='1' AND RaidMe='1'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
             // output data of each row
             while($row = $result->fetch_assoc()) {
                  array_push($chanarray, $row['TwitchNames']);
            }
        } else {
           echo "0 results";
        }
        array_splice($chanarray, 0, 1);
        $conn->close();
        echo "<script type=\"application/javascript\">";
        echo "var channels = ". json_encode($chanarray); 
        echo "</script>";
        echo "</font>";
        echo "<form id=\"form\" method=\"post\">";
        echo "<input type=\"hidden\" name=\"view\" value=\"page2\">";
        echo "</form>";
    break;

    case 'page2':
        echo "<font size=\"+3\" color=\"#FFFFFF\">Who should I host?<br>";
        echo "Your channel to host is:<br></font>";
        echo "<font size=\"+2\">";
        echo "<br><br>";
        $chans[] = null;
        $test = $_POST['chans'];
        foreach ($test as $chan) {
            $temparray = array(rand(),$chan);
            array_push($chans, $temparray);
        }
        array_splice($chans,0,1);
        sort($chans);
        echo "<a href=\"https://twitch.tv/".$chans[0][1]."\" target=\"_blank\">".$chans[0][1]."</a>";
        echo "<br><br><br>";
        echo "<a href=\"whoslive.htm\" target=\"_parent\">See All Live Channels</a>";
        echo "</font>";
    break;
}
?>

After working with what blokeish has suggested I've modified the javascript file working out where the problem is. 处理过大胆的建议后,我修改了javascript文件,找出问题所在。

The new javascript file is: 新的javascript文件是:

// JavaScript Document

var chans = ["test1","test2","test3"];



function submitchans(){
  for (var i=0;i<chans.length;i++)
  { var newHidInp = document.createElement('input');
        newHidInp.type  = 'hidden';
        newHidInp.name  = 'chans[]';
        newHidInp.value = chans[i];
     document.getElementById('chansform').appendChild(newHidInp);
  }
}
jQuery(function ($) {
        submitchans();
        document.getElementById('chansform').submit();

});

Using only the javascript clicking submit, it passes to the next page. 仅使用javascript单击提交,它将传递到下一页。 When adding in the array pass is when it fails. 当添加数组通过时是失败的。 This is the page log that is returning during the execution. 这是执行期间返回的页面日志。 !!--CORRECTION--!! !! - 更正 - !! there was a typo in the code, after correcting ID to Id the code is working as intended. 在将ID改正为ID之后,代码中出现了错字,代码已按预期工作。

阵列错误

document.forms[0].submit() is likely submitting the wp search form as that could be the first form in the DOM. document.forms [0] .submit()可能会提交wp搜索表单,因为它可能是DOM中的第一个表单。 I see " http://kltools.net/ ? s= " in the URL where "s" is the search term. 我在URL中看到“ http://kltools.net/?s = ”,其中“ s”是搜索词。

Using document.getElementById('idOfForm').submit() should get you around that problem if there are multiple forms in a page and you cant be sure of its index. 如果页面中有多种形式,并且不能确定其索引,使用document.getElementById('idOfForm')。submit()应该可以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM