Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML form with post not calling PHP script in action

I have a form in a HTML file that will post values from the form into a database, via a PHP script.

Unfortunately, when I click the submit button, my form will not call the PHP script. However, if I type in the URL of the PHP file, the PHP file will run and insert a blank row in the database. Therefore, my guess is that my HTML file never calls the PHP script.

Since I am new to PHP and HTML forms, is there any improvements or mistakes I can correct in my code?

Here is my HTML form:

<form id="frmRegister" action="dbase/insert.php" method="post" >
<table>
    <tr>
        <th><br /></th>
        <td><br /></td>
    </tr>
    <tr>
        <th align="left">First Name:</th>
        <td><input type="text" name="FirstName" /></td>
    </tr>
    <tr>
        <th align="left">Last Name:</th>
        <td><input type="text" name="LastName" /></td>
    </tr>
    <tr>
        <th align="left">Email Address:</th>
        <td><input type="text" name="Email" /></td>
    </tr>
    <tr>
        <th align="left">Phone Number:</th>
        <td><input type="text" name="Phone" maxlength="10"/></td>
    </tr>
    <tr>
        <th align="left">Username:</th>
        <td><input type="text" name="Username" maxlength="10"/></td>
    </tr>
    <tr>
        <th align="left">Password:</th>
        <td><input type="password" name="Password" maxlength="10"/></td>
    </tr>
    <tr>
        <th align="left">Re-Enter Password:</th>
        <td><input type="password" name="Password2" maxlength="10" /></td>
    </tr>
    <tr>
        <th><br /></th>
        <td><br /></td>
    </tr>
    <tr>
        <td align="center"><input type="button" name="btnSubmit" value="Submit" onclick="Validate();"/></td>
        <td align="center"><input type="reset" value="Reset" /></td>
    </tr>
</table>
</form>

Here is my PHP code:

<?php
// Connect to MySQL
$db = mysql_connect("", "nemo008", "CimesFungo");

if (!$db)
{
    exit("Error - Could not connect to MySQL");
}

$er = mysql_select_db("cs329e_fall11_nemo008", $db);

if (!er)
{
    exit("Error - Could not select the database");
}

$query = "INSERT INTO tblMembers (Username, Password, FirstName, LastName, Email, Phone, Year, Major)
VALUES ('$_POST[Username]', '$_POST[Password]', '$_POST[FirstName]', '$_POST[LastName]', '$_POST[Email]','$_POST[Phone]','$_POST[Year]','$_POST[Major]')";

$result = mysql_query($query);

if (!$result)
{
    print("Error - The query could not be executed");
    $error = mysql_error();
    print("<p>:" . $error . "</p>");
    exit;
}

echo("1 record added");

?>

1 Answers

The button type for the submit is wrongly defined.
It should be :-

<input type="submit" ...// instead of type="button"

You can take a look how the input tag works :- https://developer.mozilla.org/en/HTML/Element/input

like image 144
ajreal Avatar answered May 18 '26 16:05

ajreal