0

I am currently trying to make a HTML form page [main.html], which will take following input :

Name, Email, and Contact Number

when the data is submitted, it will go to PHP script [validate.php] where it will be validated. like whether the name contains any invalid character set or number etc.

if the validation fails, it should return error msg e.g "Data Validation Fails". and if the validation is successful, PHP page should return the data to main.html where the received data needs to be displayed and user is asked to confirm it again.

Once the data is confirmed by the User, it will be send data to another PHP Script file [store.php] for storing in a text file [e.g. UserDb.txt].

I am very much new to PHP and HTML with no knowledge of JQuery. I can somehow prepare main.html and store.php but heavily confused for validate.php page.

Please tell me how can i send back the data from PHP page [validate.php] to HTML page [main.html] to ask for confimation ??

Please do not suggest solutions involving JQuery and AJAX. There are lot of webpages for that which I can find on Internet, but i could not find any solution particularly for this case. I Hope it is possible to send data back to HTML Page from PHP Script.

3 Answers 3

1

Form action tag is there for that action. Lets see on an example. here we have our form main_form.php;

<form action="validation.php" method="post">
<h2>Form</h2>
<span class="error">* required field.</span>
Name:
<input name="name" type="text" value="">
<span class="error">* <?php echo $nameError;?></span>
E-mail:
<input name="email" type="text" value="">
<span class="error">* <?php echo $emailError;?></span>
Gender:
<input name="gender" type="radio" value="female">Female
<input name="gender" type="radio" value="male">Male
<span class="error">*<?php echo $genderError;?></span>
Website:
<input name="website" type="text" value="">
<span class="error"><?php echo $websiteError;?></span>
Comment:
<textarea cols="40" name="comment" rows="5">
</textarea>
<input  name="submit" type="submit" value="Submit">
</form>

Now lets see how we validate on validation.php;

<?php
// Initialize variables to null.
$nameError ="";
$emailError ="";
$genderError ="";
$websiteError ="";
// On submitting form below function will execute.
if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
$nameError = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameError = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailError = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid or not
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailError = "Invalid email format";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// check address syntax is valid or not(this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteError = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderError = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//php code ends here
?>

If there is an error this script will return error to the our main_form.php.For errors use css to show them in red like below error.css;

.error{
color:red
}

TL;DR: Set form action to validation.php. Keep it simple.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your inputs, however I was more interested to know the data transaction part between client [main.html] and server [validation.php and store.php]. How that can be achieved with any other simplest method ??
0

To start and set a session variabl :

session_start(); //at top of page

$_SESSION['name_of_field'] = $_POST['name_of_field']; //do checks on the post data!

To use a session varible :

session_start(); //at top of page

$my_new_value = $_SESSION['name_of_field'];

you will get data in varible.

Or you can send data via URL
Write this in php

 header('Location: http://yoursite.com/page2.php?name='.$name.'&email='.$email);

and get

$name =  $_GET['name'];
$email =  $_GET['email'];

Comments

0

You may also try

Rename your main.html to main.php then include validate.php above your form tag to display errors above form. Thus, your form action would be self or blank

<?php include "validate.php"; ?>

<form action ="" method ="post"... >

But if there are no errors, then from validate.php you can redirect to the index.php for user confirmation using

header('Location: index.php?name='.$name);

The value of name can be accessed using $_GET['name'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.