Jump to content

PHP, $_POST and Javascript!


incog

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.