Oen44
Oen44

Reputation: 3206

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?

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?

@Edit
I'm using store.js to save player arrays value (Local Storrage). 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
With Skamielina advice I did something like this:

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

And top15.php has this:

<?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

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


Stupid me... Not Array, I mean Variables. God, sorry for this mistake :x

Upvotes: 0

Views: 435

Answers (7)

Oen44
Oen44

Reputation: 3206

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/>";
?>

Upvotes: 0

Nico
Nico

Reputation: 7256

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. 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");
});

Upvotes: 1

I_G
I_G

Reputation: 413

Ajax without JQuery

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

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

Upvotes: 1

cesar moro
cesar moro

Reputation: 168

Hi you question is very open to a lot of content, you need to try to search a little before post on 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.
I recommend to you use jQuery framework i add you a tutorial for Ajax web crud web app.

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

Hope it's helps you.

Upvotes: 1

Skamielina
Skamielina

Reputation: 802

Use jQuery and $.ajax function (see 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.

Upvotes: 6

yizzlez
yizzlez

Reputation: 8805

Create a new php page that takes the Name, Class and Level information through GET or POST parameters:

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

Then, use mysqli to store such information in the database. Finally, communicate with this page using jquery's ajax.

Upvotes: 1

Hatem Jaber
Hatem Jaber

Reputation: 2402

You'll need to use AJAX to send it to the server and process it on the server using PHP and 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.

Upvotes: 2

Related Questions