I have finish my validation form and everything working fine, but I have problem on how to transfer the data from the form to another .php page which is the process page.
I`m currently refer from this site for form validation code http://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_required
myform
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" id="form">
<fieldset>
<td>Name</td>
<label>
<input type="text" name = "nama">
</input>
<span class="error">* <?php echo $nameErr;?></span>
</label>
<br>
<p>Gender</p>
<p>
Male</p><input type="radio" name= "gender" value="Male" >
<br>
<input name= "gender" type="radio" value="Female">
Female<br></input>
<span class="error">* <?php echo $genderErr;?></span>
<br>
<td>Contact No</td>
<label>
<input type="text" name= "contact">
</input>
<span class="error">* <?php echo $contactErr;?></span>
</label>
<br>
<td>Address</td>
<label>
<textarea name= "address"></textarea>
<span class="error">* <?php echo $addressErr;?></span>
</label>
<br>
<td>Email</td>
<label>
<input type="text" name= "email"></input>
<span class="error">* <?php echo $emailErr;?></span>
</label>
<br>
<td>Account Name</td>
<label>
<input type="text" name= "account"></input>
<span class="error">* <?php echo $accountErr;?></span>
</label>
<br>
<td>Password</td>
<label>
<input type="password" name= "password"></input>
<span class="error">* <?php echo $passwordErr;?></span>
</label>
<br>
<input type="button" value="Cancel"onClick="history.go(-1);return true;">
<input type="submit" value="Submit">
</fieldset>
</form>
process.php
<?php
$link = mysqli_connect('localhost', 'root', '', 'sport');
$nama=$_POST["nama"];
$gender=$_POST["gender"];
$contact=$_POST["contact"];
$address=$_POST["address"];
$email=$_POST["email"];
$account=$_POST["account"];
$password=$_POST["password"];
$sql = "INSERT INTO sport(name, gender, contact, address, email, account, password, eventtype) VALUES ('$nama' , '$gender', '$contact' , '$address', '$email', '$account', '$password', '')";
$query=mysqli_query($link, $sql);
if (!$query)
{
echo "Fail to register";
}
else
{
header('Location:login_success.html');
}
?>
How do I pass the data after I validate the form to process.php and execute the sql query so I store the data into the database. Supposedly we just put the target page in , but the code required me to use <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
As per the link you have provided that they have used a server side validation instead of doing the server side validation try to do a client side validation by using the JavaScript or jQuery.
you can check the simplest way of doing the JavaScript validation here : http://www.w3schools.com/js/tryit.asp?filename=tryjs_validation_js
or you can try this by using any jquery library for form validation:
for demo : http://formvalidator.net/#reg-form
Your form action is pointing to itself, so I'll assume the validation code is above the snippet you posted (as shown in the link you provided).
When you send a POST HTTP request to PHP, it'll store form data in an associative array at superglobals $_POST
and $_REQUEST
. The script referenced on "form action" is the "owner" of $_POST
content, meaning other non-related scripts won't be able to access it. To solve it, you should require
or include
the process.php script into your form, so it can manipulate $_POST
content as well (beware your code flux!).
Please, note this is just to answer what you're asking, but it's not the best way to solve your problem. Form validation processes are better approached client-side with asynchronous methods such as AJAX as you can see in this tutorial. Also, check out PHP form good practices here.