Jump to content

Variables With Php And Javascript


FARGORE
 Share

Recommended Posts

I’m using AJAX to edit a web page and when the page is edited I need to activate or deactivate certain buttons based on the results. The problem is that the conditions for changing the buttons is based on variables in PHP since they were originally pulled from a database. The AJAX call is to a .php that constructs output and sends it back. I tried to send the PHP Variable to JavaScript within the function call to the JavaScript and could not get it to work.

 echo "<button type=\"button\" id=\"nexpage\" onclick=\"nextpage(1," . <?php  Variable ?> . " )\">Next Page</button>"; 

I also tried embedding PHP into the JavaScript function (which I have successfully done in reverse before) to give the condition and switch back to JavaScript for the result code. That also did not work.

</script><?phpif ( PHPVeriable == 0){?><script language="javascript">document.getElementById("prepage").disabled=false;document.getElementById("nexpage").disabled=true;</script><?php } ?><script language="javascript">

This code works to embed JavaScript into PHP if the JavaScript open and close tags are not at the beginning and end, but does not work to embed the PHP into JavaScript I also tried to give the button two onclick functions, one to call the JavaScript for the AJAX and one to call a PHP with the conditions and embedded JavaScript to execute the actual change. I could not get this to work either.

<button type="button" id="nexpage" onclick="Javafunction(1)" onclick="PHPfunction(1)">Next Page</button>

Is there any way of passing a PHP Variable to a JavaScript with a function call or any other way of giving JavaScript access to a PHP Variable, a way of embedding PHP conditional code into a JavaScript function, a way of using PHP to activate or deactivate a button, or a way of giving a button two onclick functions. I do believe I’m getting better at web programming and I’m trying to ask for help as little as possible since the members of this forum are so kind and helpful. So I tried everything I could think of before breaking down and posting this. Thank you very much in advance for any advice I appreciate it very much.

Edited by FARGORE
Link to comment
Share on other sites

echo "<button type=\"button\" id=\"nexpage\" onclick=\"nextpage(1," . <?php Variable ?> . " )\">Next Page</button>"; Since you're using echo, you don't need to have <?php Variable ?>. I'm assuming the above is being echoed inside PHP tags already. So instead of <?php variable ?>, $variable or whatever your variable is named goes there.

Edited by Don E
  • Like 1
Link to comment
Share on other sites

And just like that it’s fixed! Thank you so much after staring at that code for so long I feel stupid that I missed that. So if no one minds me asking, just in case I should run into a situation where this would be helpful, is there a way of creating a button with two onclick functions, one that calls a php and one that calls a JavaScript? Regardless thank you very much for the answer, it fixed the problem.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...