Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Passing PHP variables back to jQuery

jquery

  • Please log in to reply
7 replies to this topic

#1 datalogi

datalogi

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 07 December 2011 - 09:19 AM

Hi, could someone help me how to pass following variables back to jQuery from PHP-file,
which I was called from jQuery.

$sql = $dbh->prepare("SELECT merkki, vuosimalli FROM autot");
$sql->execute();
$row = $sql->fetch();
$merkki = $row['merkki'];
$vuosimalli = $row['vuosimalli'];
  • 0

#2 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 07 December 2011 - 09:26 AM

We'll need to see how you cann this page with jquery
  • 0

#3 datalogi

datalogi

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 07 December 2011 - 10:09 AM

Ok, let me explain.

This is jQuery code:
function search () {
$.get("search.php?ID=", function(data){
// return data from PHP file
});
}

And this is PHP-file search.php
<?php
$sql = $dbh->prepare("SELECT merkki, vuosimalli FROM autot");
$sql->execute();
$row = $sql->fetch();
$merkki = $row['merkki'];
$vuosimalli = $row['vuosimalli'];
?>
  • 0

#4 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 07 December 2011 - 10:23 AM

In this case, simply do a echo of your variable, and it will be in the variable data
  • 0

#5 datalogi

datalogi

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 07 December 2011 - 10:40 AM

But i need to echo to variables: $merkki and $vuosimalli

I think it's possible to echo only one variable....?
  • 0

#6 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 07 December 2011 - 10:47 AM

echo($merkki);
echo($vuosimalli);
To split them back in your jquery you could separate them with a comma, or any other char...

If you'd like a more elegant method, you chould do something like this
echo(json_encode(array('merkki' => $merkki, 'vuosimalli' => $vuosimalli)));
and in your jquery you read it with jQuery.parseJSON(data);
  • 0

#7 datalogi

datalogi

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 08 December 2011 - 02:54 AM

So now I have:
$character = " | ";

echo($merkki);
echo($character);
echo($vuosimalli);


jQuery output is: "Toyota | 2008"

So how can I parse Toyota to one jQuery variable and 2008 to another jQuery variable?
  • 0

#8 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 08 December 2011 - 02:04 PM

Do it the JSON way described above and fix the rest in the javascript, i'd say.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download