Hi, I am trying to have a javascript function to mix with php inside of it. But I can't get the syntax right for the line of code with the mysql_query. How do we use the variable created in javascript in php? Any tip to fix this will be highly appreciated. <script type="text/javascript"> function insertname() { var name ='Jane Smith'; <?php mysql_query("INSERT INTO students (student_name) VALUES ('".?>name<?php ""."')"); ?> } </script>
You cannot mix Javascript and PHP. To do what you want you need to do an ajax request which will call a PHP Script where you can run your mysql query. Just search on google for jquery ajax or read this article which will give you a detailed how to on how to use AJAX: http://articles.sitepoint.com/article/ajax-jquery
Nyu, thank you very very much for the tip. I just read through the tutorial of jquery at w3schools.com.( I always start learning new things there and plan to go to the link in your response later). One more question here. What if I already have something like the following and it has some value in it through ajax already before the mysql part. Can I use it with the php mysql code? If so, how? Thanks for any help in advance. ....document.getElementById("name").innerHTML....
I'm not sure if i understand your question correctly... So you want the inner html value fom an element passed to your mysql query? Then you would write something like this (I assume that the insertname function will start the whole ajax request): The javascript code <script type="text/javascript"> function insertname(id) { // will get the inner html from the element with the id specified in the variable id var name = $('#' + id).html(); // submit ajax request $.post("test.php", { name: name }, function(data) { alert("Status: " + data.status); }, "json"); } </script> HTML: Then your test.php would have the following code: <?php // establish a connection to your database here... // insert name into db mysql_query("INSERT INTO students (student_name) VALUES (".$_POST['name'].")"); echo json_encode(array("status"=>"ok")); ?> PHP: Then somewhere in your code you have to call the insertname function with the id of the element you want to use the inner html from. It will do an ajax request ($.post) calling the php script specefied in the first parameter ("test.php") with the post parameters given in the second parameter ({ name: name } Note: Syntax is variableName: value. So if you would write { myname: "John" } in the PHP script you could acces this value with $_POST['myname']). After the ajax request is complete the function from the third parameter will be called. Just a simple alert in this case You can get more information about the jquery methods at http://docs.jquery.com/
Nyu, thank you very much for your response. Before I confuse myself with all the excellent jquery code your provided nicely, (will learn more about jquery soon) please allow me to explain my intention. I already had several functions doing the ajax thing to have a value in the innerHTML for "name". I am wondering if I can do something like this, (of course there is some error here) mysql_query("INSERT INTO students (student_name) VALUES ('".?>document.getElementById("name").innerHTML<?php ""."')"); Thanks for any help in advance.
you have to use AJAX to achieve it. You can try JQuery and Dojo. Both of those libraries provide good and easy Ajax method. you can't insert javascript variabel into PHP function.
my above code snippets exactly does what you want You just have to pass the id of the element you want to use the inner html from to the function insertname i wrote in my above post. For your example with the id "name" it would look like this: insertname("name"); Code (markup): Then this Javascript function will search for the elemente with the id "name", get its inner html value and fire an ajax request that calls the php script named test.php. Within the PHP Script you can then do your mysql query. And as you passed the inner html value to the php script as the third paramater in the jquery post method (see the part with $.post in my post above) you can use it in the PHP script via the variable $_POST['name']
If you do not want to use AJAX, you can set the value of a hidden field of a form and submit the form with the action set to the same page, then get the var via $_POST
I dont use post.. instead: function submitform(iso_country) { var formm = document.forms["myform"].elements["city"].value;var iso_c=iso_country; document.location.replace("i.php?f="+formm+"&c="+iso_c); }</script>'; <form name="myform" action="javascript: submitform('FR')"> city: <input type="text" name="city" /> <a href="javascript: submitform('FR')">Search</a> </form>