9 Replies - 8386 Views - Last Post: 08 February 2013 - 09:11 AM

#1 childlucy00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-February 10

Passing variables between ajax and php

Posted 08 February 2013 - 07:49 AM

Hi, I'm currently in the process of making a game in Javascript, and I am trying to pass variables into php to go into a database. I have code to insert the variables working fine. but when I try to search the database I hit a snag.

I am using this code to get data from the table:
 $.ajax({                                       

      url: 'includes/search.php',                  //the script to call to get data          

      type:"POST",
	 
	  //data: {email: email},                        //you can insert url argumnets here to pass to api.php

                                       //for example "id=5&parent=6"

      dataType: 'json',                //data format      

      success: function(data)          //on recieve of reply

      {

        score = data[3];              //get id

        name = data[2];           		//get name

      } 

    });


and this php with the necessary connection requirements not posted for security reasons:

$email = @$_GET['email'];
$result = mysql_query("SELECT * FROM $tableName WHERE Email = $email");
$array = mysql_fetch_row($result);
echo json_encode($array);  



However, I don't have a way to pass a search variable into the php and can only search the db if I replace $email with an actual email address (which works, so the code in the php is fine.

Can anyone let me know a way to do this.

thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Passing variables between ajax and php

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9363
  • View blog
  • Posts: 35,172
  • Joined: 12-June 08

Re: Passing variables between ajax and php

Posted 08 February 2013 - 08:09 AM

Typically there's quite a few ways of passing data to a php file. You can have the page navigate there directly and pass via the session variable.. or you can use ajax and do it more quietly. Remember you'll need to add a jquery javascript reference to your project.

http://api.jquery.com/jQuery.ajax/

the basic form is:

$.ajax(
{
 type : <string of "POST" or "GET">,
 url : <your php file>,
 data : 
 {
  <string name of variable you are passing> : <value> 
  ,(repeat as needed)
 },
 datatype : <string of "xml", "json", "script", or "html" >,
 success : function(data)
 {
   <a function to deal with what ever is returned
 }
});



Say your 'type' is "POST" and your data was a variable named "clientid". Your PHP would start off looking like this:

<?php

$clientid = $_POST["clientid"];




Pretty straight forward, right?

I have a few examples floating around in this project:
http://www.dreaminco...-collaboration/


Side note - stop using the "mysql_" calls and use PDO.
Was This Post Helpful? 0
  • +
  • -

#3 childlucy00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-February 10

Re: Passing variables between ajax and php

Posted 08 February 2013 - 08:26 AM

Ok, thanks. I've tried doing what you said...

email variable is entered earlier in the process, and it works, as it's being added to the db
 $.ajax({  
		 
		 type: "POST",                                     

      url: 'includes/search.php',                  //the script to call to get data          

	  data: {"email": email},                        //you can insert url argumnets here to pass to api.php

                                       //for example "id=5&parent=6"

      dataType: 'json',                //data format      

      success: function(data)          //on recieve of reply

      {

        score = data[3];              //get id

        name = data[2];           		//get name

      } 

    });


and the php now uses POST, not GET
$email = $_POST["email"];



but it still isn't working.

The last 2 lines of the ajax where the data array is passing data into variables isnt working. the console is bringing up that the array is empty?

Thanks again
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9363
  • View blog
  • Posts: 35,172
  • Joined: 12-June 08

Re: Passing variables between ajax and php

Posted 08 February 2013 - 08:29 AM

Okay then something is whack in your php.

Thought to check - you did *ADD* the jquery reference at the top of your html file, right?
Was This Post Helpful? 0
  • +
  • -

#5 childlucy00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-February 10

Re: Passing variables between ajax and php

Posted 08 February 2013 - 08:35 AM

When you say the jquery reference did you mean
<script type="text/javascript" src="libs/jquery-1.8.3.min.js"></script>


?
In which case, yes
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9363
  • View blog
  • Posts: 35,172
  • Joined: 12-June 08

Re: Passing variables between ajax and php

Posted 08 February 2013 - 08:39 AM

Cool.. as long as you have that file there.

Now as for debugging this process I sort of went the long way around. I started with passing a numeric value in, in the php added some value like 23 to it, returned it, and had the success function just throw up the new value in an alert. Once that was verified to work then I started the more complex mysql interactions..
Was This Post Helpful? 0
  • +
  • -

#7 childlucy00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-February 10

Re: Passing variables between ajax and php

Posted 08 February 2013 - 08:46 AM

If I replace the bit in the ajax where the data is passed

data: {Email : email}

with

data: {AccountID : 1}

and then change the php so that it searches the db for where the Account ID = 1, it works.

So the problem is passing the email address, although I'm not sure why!
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9363
  • View blog
  • Posts: 35,172
  • Joined: 12-June 08

Re: Passing variables between ajax and php

Posted 08 February 2013 - 08:48 AM

well.. we can't help ya unless we see the php code. ;)
Was This Post Helpful? 0
  • +
  • -

#9 childlucy00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-February 10

Re: Passing variables between ajax and php

Posted 08 February 2013 - 08:52 AM

sorry :)

<?php 
  $host = "**********";

  $user = "************";

  $pass = "**********";



  $databaseName = "*******";

  $tableName = "DAA";

  $email = $_POST["email"];
 
  $con = mysql_connect($host,$user,$pass);

  $dbs = mysql_select_db($databaseName, $con);

  $result = mysql_query("SELECT * FROM $tableName WHERE Email = $email");          //query

  $array = mysql_fetch_row($result);                          
  echo json_encode($array);
?>


Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9363
  • View blog
  • Posts: 35,172
  • Joined: 12-June 08

Re: Passing variables between ajax and php

Posted 08 February 2013 - 09:11 AM

Ah.. line 20. First - it is bad, very, very bad to just throw in some value. That's opening the doors for bad guys with sql injection and handing them out freshly made pancakes when they come barging in. Always *ALWAYS* used parametrized queries.

but.. working with what you have there let's break down what you are asking.

 $result = mysql_query("SELECT * FROM $tableName WHERE Email = $email"); 


I am guessing what you think is happening is you are getting all the columns from DAA where the column email equals some value, right? Noooooot quite.

Let's say your $email is "foo@bar.com". Your select statement is reading as

SELECT * FROM $tableName WHERE Email = foo@bar.com   


Do you see the problem? You are not telling mysql that "foo@bar.com" is a string of characters. It is treating it like a column name. You need to add tick marks around it.

 $result = mysql_query("SELECT * FROM $tableName WHERE Email = '$email' "); 


Also, if you check the documentation for the depreciated 'mysql_query' you will find that if there is an error your $result will be "false".

Quote

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.


You should *ALWAYS* be checking for this *RIGHT AFTER* you make a call and then if $result is false check the " mysql_error()" to find out the error. It is a prime debugging habit to get into... so instead of fumbling for what is going on you are explicitly told.

http://www.php.net/mysql_query

Side note - why is the table name in a variable? That seems odd.
Side side note - select * is generally a bad practice. Enumerate your column names!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1