Ok friends I am very confused with this basic thing. I have read lot of posts where it says, I need to add SELF POST or something but I am not understanding.
I have two files, index.html and submit.php. index.html has a form with a submit button by clicking which, the submit.php file is called and shows a message "1 record added". I want to redirect back from the submit.php file to index.html file. I dont know what I am doing wrong. Is it because one is html file and another php? Please help. Here is my code
index.html file
<form method="post" action="submit.php">
submit.php file
<?php
$con = mysql_connect("localhost","myuser","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$sql="INSERT INTO members (sName, sCity, sMobile, sEmail, sSub, sSlogan)
VALUES ('$_POST[name]', '$_POST[city]', '$_POST[mobile]', '$_POST[email]', '$_POST[sub]', '$_POST[slogan]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
</body>
</html>
EDIT
Please find code of index.html and submit.php file. With your help, both are working perfect. I am still struggling where excatly to put the validation code. i am using html5 input types in html file and I dont know where exactly the validation to happen in submit.php file. Yes, I do have multiple forms and I made validations.php file as suggested by you. What I am not understanding is if you have function validate_name($input) for name field in validations.php file then why are you validating name again in submit.php? ( if (!empty($_POST['name']) ). I am also not understanding where will the error messages been shown? If I try to add these functions, it gives me blank page on clicking submit and the data does not go to database.
Can you suggest a location in the submit.php file where I should add these validations by editing my submit.php file?
Regexp for email ('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'
)
Regexp for phone (^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$
)
This is my index.html file
<!DOCTYPE HTML>
<html>
<head>
<title>My Title</title>
</head>
<body>
<form method="post" action="submit.php">
<div class="box">
<div class="cl"><input type="text" name="name" placeholder="Name" /></div>
<div class="cl"><input type="text" name="city" placeholder="City" /></div>
<div class="cl"><input type="text" name="mobile" placeholder="Mobile" /></div>
<div class="cl"><input type="email" name="email" placeholder="Email" /></div>
<div class="cl"><input type="text" name="sub" placeholder="Want 3 m Free Subscription (Yes/No)?"></textarea></div>
<div class="cl"><input type="text" name="slogan" placeholder="Suggest a slogan for 6 m subscription"></textarea></div>
</div>
<div class="srow">
<div class="cl1">
<ul class="action">
<li><input type="submit" value="Submit" /></li>
</ul>
</div>
</div>
</form>
</body>
</html>
This is the submit.php file help that I took from you
<?php
include 'config.php'; // store your configuration in a seperate file so
// you only need to update it once when your environment changes
$errors = false;
$output = '';
$nl = '<br>'.PHP_EOL;
$redirect_url = 'index.html';
if (!$con = new mysqli(DBHOST,DBUSER,DBPASS,DBNAME)){
$errors = true;
$output .= "ERROR Can't connect to DB".$nl;
};
if (!$errors){
//should validate/clean $_POST before using in query
$name = $con->escape_string($_POST['name']);
$city = $con->escape_string($_POST['city']);
$email = $con->escape_string($_POST['email']);
$mobile = $con->escape_string($_POST['mobile']);
$sub = $con->escape_string($_POST['sub']);
$slogan = $con->escape_string($_POST['slogan']);
$sql="INSERT INTO members
(sName, sCity, sMobile, sEmail, sSub, sSlogan)
VALUES ('$name', '$city', '$mobile', '$email',
'$sub','$slogan')";
if (!$con->query($sql)){ //forgot a parenthesis here earlier
$output .= 'ERROR: DB said: ('.$con->errno.') '.$con->error.$nl;
$output .= 'Query was:'.$sql.$nl;
$errors = true;
}else{
$output .= "1 record added".$nl;
}
}
if (!$errors){
//if there are no errors redirect to index.html;
header('refresh: 2; URL='.$redirect_url);
$output .= '...Redirecting...'.$nl;
}else{
//show the errors and allow display a link to go back/try again
$output .= '<a href="'.$redirect_url.'">Try again</a>'.$nl;
}
echo $output;
?>
PS: One thing I observed is that html5 (input type="email") shows invalid email alert immediately after you go to next field, just under the field. How is it possible to do it for all fields? (something like a validation check on field lost focus)
Thanks