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'];
7 replies to this topic
#1
Posted 07 December 2011 - 09:19 AM
|
|
|
#2
Posted 07 December 2011 - 09:26 AM
We'll need to see how you cann this page with jquery
#3
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'];
?>
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'];
?>
#4
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
#5
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....?
I think it's possible to echo only one variable....?
#6
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);
#7
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?
$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?
#8
Posted 08 December 2011 - 02:04 PM
Do it the JSON way described above and fix the rest in the javascript, i'd say.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









