incog Posted December 12, 2008 Share Posted December 12, 2008 I am writing a project website which makes use of PHP, Javascript and MySQL. The website will allow me to manage my contact information. The problem I'm having, involves accessing the user-entered text in a textbox in a FORM. Say I try to print the contents of the text box: print $_POST['txtLastName']; ... nothing is printed. No value is given. The PHP code is located within a Javascript function located in the HEAD section of my page. What happens is the user types in a last name in a text box, clicks a BUTTON object (but not a 'submit' button), and this button calls a Javascript function from the buttons onClick() method. It seems a bit messy, but when the user clicks the button, I want PHP to interact with MySQL by retrieving a table's contents, then all of the table's rows will be checked to see if the name the user entered into the text box already exists in the table. I've created a simple example of what is causing me grief: <html> <head> <script language="javascript"> function printName() { <?php print "alert('" . $_POST['txtLastName'] . "');"; ?> } </script> </head> <body> <form method="POST"> <input type="text" name="txtLastName"> <input type="button" name="btnContinue" value="Continue" onClick="printName()"> </form> </body> </html> When the user clicks the button after entering a value, an alert box appears, but without a value! Why isn't PHP getting a value for the txtLastName textbox? Do I need to have the user click a SUBMIT type button and have the page reloaded? I want to perform the check on the database table to see if the last name already exists, but without reloading the page. I've read that AJAX might be an answer, but is there any other way? Thanks! Link to comment https://forums.phpfreaks.com/topic/136599-php-_post-and-javascript/ Share on other sites More sharing options...
laPistola Posted December 12, 2008 Share Posted December 12, 2008 you cant use php inside java like you are, the only way without ajax is to have the php varable in the url on a page reload and use java to read the url varable Link to comment https://forums.phpfreaks.com/topic/136599-php-_post-and-javascript/#findComment-713231 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 laPistorla is right but i think he means javascript not java (2 different things) Your best option is to add action="anewpage.php" to your form. On that new page run your php Link to comment https://forums.phpfreaks.com/topic/136599-php-_post-and-javascript/#findComment-713234 Share on other sites More sharing options...
laPistola Posted December 12, 2008 Share Posted December 12, 2008 yes i did mean javascript sorry, i blame my man flu :s Link to comment https://forums.phpfreaks.com/topic/136599-php-_post-and-javascript/#findComment-713236 Share on other sites More sharing options...
haku Posted December 12, 2008 Share Posted December 12, 2008 function printName() { alert(document.getElementById(txtLastName).value) } <input type="text" id="txtLastName" name="txtLastName"> <input type="button" name="btnContinue" value="Continue" onClick="printName()"> Link to comment https://forums.phpfreaks.com/topic/136599-php-_post-and-javascript/#findComment-713269 Share on other sites More sharing options...
incog Posted December 12, 2008 Author Share Posted December 12, 2008 haku, I definitely would have used that method, but I need to access the value of the textbox in PHP, as I have PHP code in the same Javascript function which accesses a MySQL database. I want the PHP code which takes the value in the textbox, retrieves all rows from a database table, compares the text box value against all of the table rows (in specific, the 'last_name' column), and then tells the user whether the name already exists. I guess I was hoping that after the page initially loads, then the user enters a name in the text box and then clicks the button (which is NOT a submit button, thus the page does not reload), a Javascript function is called, which has embedded PHP to make use of the MySQL database. Since PHP can't access the form object's value until after the page has been submitted with the newly entered values to the server, I'll need Ajax or just go ahead and do what laPistola and gevans mentioned, and simply have the user click a Submit button and send data to a new page. Link to comment https://forums.phpfreaks.com/topic/136599-php-_post-and-javascript/#findComment-713305 Share on other sites More sharing options...
haku Posted December 12, 2008 Share Posted December 12, 2008 You want AJAX then (there is an ajax section on this forum). It uses javascript in the background to make a call to a script on the server, then uses the returned data on your web page. Link to comment https://forums.phpfreaks.com/topic/136599-php-_post-and-javascript/#findComment-713313 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.