Remus Rigo
Remus Rigo

Reputation: 1464

Detect click on submit button in PHP

I have a php file that contains a form (which contains 2 input boxes and a submit button) for updating a contact. I managed to fill the fields with the contact's data, but I can't detect if the submit button is clicked

form looks like this

echo "<form action=Contact.php><table>".
"<tr><td>First Name</td><td><input type=text size=75% name=FirstName value='".$row['FirstName']."'></td></tr>".
"<tr><td>Last Name</td><td><input type=text size=75% name=LastName value='".$row['LastName']."'></td></tr>".
"<tr><td colspan=2><input type=submit name=UpdateContact value=Update></td></tr>".
"</table></form>";

this code should output a "clicked" message if the button is clicked

if (isset($_POST['UpdateContact']))
{
   echo "<p>clicked";
}
else
{
   echo "<p>not clicked";
}

can anyone help me out or tell me what i've done wrong

(I want from the same php file to fill the contact's data in a from and to update the database)

Upvotes: 1

Views: 15622

Answers (2)

Matt
Matt

Reputation: 75327

The default method for a form is GET, so either set the form's method attribute to "post", or change your $_POST in PHP to $_GET.

echo "<form method=post action=Contact.php><table>".
"<tr><td>First Name</td><td><input type=text size=75% name=FirstName value='".$row['FirstName']."'></td></tr>".
"<tr><td>Last Name</td><td><input type=text size=75% name=LastName value='".$row['LastName']."'></td></tr>".
"<tr><td colspan=2><input type=submit name=UpdateContact value=Update></td></tr>".
"</table></form>";

or

if (isset($_GET['UpdateContact']))
{
   echo "<p>clicked";
}
else
{
   echo "<p>not clicked";
}

Upvotes: 5

Kapil D
Kapil D

Reputation: 2660

are you sure your code is reaching the php file. If yes, then the first think to check is the request type.. using $_SERVER['REQUEST_METHOD']

it will give POST in case the button was click and GET in case it the page was accessed using URL like.. google.com/Contact.php. also, u might ned to add METHOD ="POST" in the first line of form action "form action=Contact.php

Upvotes: 0

Related Questions