1

I have 2 files, main.html and search.php.

Within the main.html page, there is this piece of code :

$("#results").load("search.php",queryString,function(response,status,xhr){
... some code here
})

Within the search.phppage, there is this piece of code :

$image_id=mysql_insert_id() // getting the last ID of the last query - works perfectly

What I want to do is to echo this variable to the main.html file and store it within a JS variable called im_id that I can call through the main.html page.

I tried to do this

echo "<script type=\"text/javascript\">$im_id=".mysql_insert_id()."</script>";

but it doesn't work at all.

I found a workaround which is to store the content within a textbox like this

echo "<script type=\"text/javascript\">$(\"#textbox\").val(".$image_id.")</script>";

and then in the JScript, use

$im_id=$("#textbox").val();

and many other methods using an "intermediary" but isn't there a straight way to set the variables directly ?

Any help would be appreciated.

2 Answers 2

3

Its not working because you output $im_id=1, you should output var im_id = 1; instead.

In your PHP:

echo "<script type=\"text/javascript\">var im_id = ".mysql_insert_id().";</script>";

If it still doesnt work look at the generated code, not the PHP source. Just go to view source in your browser, and use a debugger to find out whats wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm so stupid sometimes lol. I knew this is what to do when the php script and jscript are in the same page but now I had so many troubles with AJAX that I completely missed this detail and thought it was an AJAX problem. Thank you a lot, it works perfectly. I'll accept the answer when the website allows me in 8 mins.
1

The reason it's not working is that php replaces variables inside double quotes with the value of your variable. This makes for easy templating. For more information see the variable parsing section of the php docs.

<?php
$juice = "apple";

echo "He drank some $juice juice.".PHP_EOL;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
?>

The above example will output:

He drank some apple juice.
He drank some juice made of .

Either replace the double quotes with single if you want to have a variable name in javascript that starts with a $:

echo '<script type=\"text/javascript\">$im_id='.mysql_insert_id().'</script>';

or, don't use a $ in your variable name:

echo "<script type=\"text/javascript\">im_id=".mysql_insert_id()."</script>";

3 Comments

Thank you for your answer, but using single quotes instead of double quotes doesn't work, also for the second option to work, I need to put "var im_id" and not just "im_id".
Actually, from the little I know (I'm a mechanical engineer, learned web developing at home), to pass a variable from PHP to JS, the variable HAS to be declared GLOBALLY. This is why I said I'm so stupid sometimes because I knew it but forgot about it since I was dealing with passing variables not directly but through AJAX.
You should be able to put im_id, but I should've mentioned it's bad practice as in javascript that creates an implicit global variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.