简体   繁体   English

如何将Javascript数组值传递给PHP,然后使用该值发送MySQL查询?

[英]How to pass Javascript array value to PHP and then send MySQL query with this value?


As I mentioned in title, how can I pass JS array value to PHP and then send it using mysqli? 如标题中所述,如何将JS数组值传递给PHP,然后使用mysqli发送它?

Here are functions that will help in getting values to be send later. 这里有一些函数将帮助您获取稍后发送的值。

function getPlayerName() {
    return player.Name;
}

function getPlayerClass() {
    return player.Class;
}

function getPlayerLevel() {
    return player.Level;
}

Most important thing for me is that how can I pass this arrays to PHP? 对我来说最重要的是,如何将这些数组传递给PHP?

@Edit @编辑
I'm using store.js to save player arrays value (Local Storrage). 我正在使用store.js保存播放器数组的值(本地存储)。 And here is function that saves it when new player is created. 这是创建新播放器时将其保存的功能。

function saveData(){
    var playerName = document.getElementById('nickname');
    var warriorClass = document.getElementById('warrior');
    var mageClass = document.getElementById('mage');
    var archerClass = document.getElementById('archer');

    if(warriorClass.checked){
        store.set('player', {
            Name: playerName.value,
            Class: 'Warrior',
            Level: playerLevel,
            XP: playerXP,
            HP: playerHP,
            MaxHP: playerMaxHP
        });
    }
    else if(mageClass.checked){
        store.set('player', {
            Name: playerName.value,
            Class: 'Mage',
            Level: playerLevel,
            XP: playerXP,
            HP: playerHP,
            MaxHP: playerMaxHP
        });
    }
    else if(archerClass.checked){
        store.set('player', {
            Name: playerName.value,
            Class: 'Archer',
            Level: playerLevel,
            XP: playerXP,
            HP: playerHP,
            MaxHP: playerMaxHP
        });
    }
}


@Edit2 @ Edit2
With Skamielina advice I did something like this: 借助Skamielina的建议,我做了以下事情:

$.ajax({
      method: "POST",
      url: "top15.php",
      data: { Name: player.Name, Class: player.Class, Level: player.Level }
    })

And top15.php has this: top15.php具有以下功能:

<?php
        echo 'Name: ' . $_POST['Name'] . '<br/>';
        echo 'Level: ' . $_POST['Level'] . '<br/>';
        echo 'Class: ' . $_POST['Class'] . '<br/>';
?>

Now, on webpage it's 现在,在网页上

Name: 
Level: 
Class: 

But, FireBug console shows 但是,FireBug控制台显示

Name: Oen<br/>Level: 1<br/>Class: Mage<br/> 


Stupid me... Not Array, I mean Variables. 愚蠢的我...不是数组,我的意思是变量。 God, sorry for this mistake :x 上帝,为这个错误感到抱歉:x

Use jQuery and $.ajax function (see docs ): 使用jQuery和$.ajax函数(请参阅docs ):

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: player.Name, class: player.Class, level: player.Level }
})

Next you need have that php file to handle request from the browser, and eventually return results. 接下来,您需要该php文件来处理来自浏览器的请求,并最终返回结果。

You'll need to use AJAX to send it to the server and process it on the server using PHP and mysqli(). 您将需要使用AJAX将其发送到服务器,并使用PHP和mysqli()在服务器上对其进行处理。 Depending on what you're using, there is a ton of libraries out there that make XHR requests, look at JQuery, it would be the easiest to use probably. 根据您所使用的内容,这里有大量的库可以发出XHR请求,例如JQuery,这可能是最容易使用的。

Create a new php page that takes the Name , Class and Level information through GET or POST parameters: 创建一个新的php页面,该页面通过GET或POST参数获取NameClassLevel信息:

$name = $_GET/$POST["Name"];

Then, use mysqli to store such information in the database. 然后,使用mysqli将此类信息存储在数据库中。 Finally, communicate with this page using jquery 's ajax . 最后,使用jqueryajax与该页面进行通信。

Hi you question is very open to a lot of content, you need to try to search a little before post on stackoverflow 嗨,您的问题对很多内容都开放,您需要尝试一些搜索后才能在stackoverflow上发布

For sending javascript data into php you have to use Ajax then into the php you can do anything you want to select, insert, update, or anything you need. 要将javascript数据发送到php中,您必须使用Ajax,然后才能在php中执行您想要选择,插入,更新或所需的任何操作。
I recommend to you use jQuery framework i add you a tutorial for Ajax web crud web app. 我建议您使用jQuery框架,并为您添加Ajax Web Crud Web应用程序的教程。

http://www.ibm.com/developerworks/library/os-php-jquery-ajax/ http://www.ibm.com/developerworks/library/os-php-jquery-ajax/

Hope it's helps you. 希望对您有帮助。

Ajax without JQuery 没有JQuery的Ajax

function yourFunction(str){

   if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","yourPhp.php?q="+str,true);
            xmlhttp.send();
}

}

and in yourPhp.php to get your value 并在yourPhp.php中获得价值

$q .= $_GET['q']; $ q。= $ _GET ['q'];

Is not clear to me if results are arrays or if you need to pass them as array. 我不清楚结果是否为数组,或者是否需要将它们作为数组传递。 As other has already said best way to do this is with Ajax and Jquery. 正如其他人已经说过的那样,最好的方法是使用Ajax和Jquery。 Check this fiddle if you need to pass them as array. 如果您需要将它们作为数组传递,请检查此小提琴

var player = new players("pippo","something",12);
var array_php = new Array();
array_php["name"] = getPlayerName(player);
array_php["Class"] = getPlayerClass(player);
array_php["level"] = getPlayerLevel(player);

//pass it to php file with jquery

$.ajax({
  method: "POST",
  url: "/echo/html/", //user your own file.php
  data: { array:array_php }
}).done(function() {
 alert("uploaded");
});

Sorry for my mistake, I meant Variables not Arrays. 对不起,我的意思是变量而不是数组。 My bad, solution is way easier than i thought. 我的糟糕解决方案比我想象的要容易得多。

<?php
        echo '<script>';
        echo "var player = store.get('player')";
        echo '</script>';
        echo "Name: <script> document.writeln(player.Name)</script><br/>";
        echo "Level: <script> document.writeln(player.Level)</script><br/>";
        echo "Class: <script> document.writeln(player.Class)</script><br/>";
?>

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

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