I have this issue with my validation and posting the data to another page.
Here is my form:
Signup.php
<form id="regForm" action="<?php echo htmlspecialchars($_SERVER["submit.php"]);?>" method="post" name="regForm">
<label for="fname">First Name:</label><input name="fname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];}?>"/><br/>
<label for="mdname">Middle initial:</label><input name="mdname" type="text" size="10" maxlength="35" value="<?php if(isset($_POST['mdname'])){echo $_POST['mdname'];}?>"/><br/>
<label for="lname">Last Name:</label><input name="lname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];}?>"/><br/>
<br/>
<label> </label><input type="submit" name="Signup" class="formButton" value="Signup" /></form>
And here is my submit.php which will validate the signup.html input
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
Now, my issue is this, in my "submit.php" file, I want to know what codes to put after the form fields validation that would enable me post the input data to another page because, I plan making it a two-page signup form. Let's say my next page is signup-2.html
how do I post the data after validation to the next page? I know how to retrieve the posted data on the next page like using Session or Echo the $_POST data but, my main issue is....how do I make the form post the data after the validation messages in my submit.php file?
use header :
header("Location: your page url?fname=$_POST['fname']&lname=$_POST['lname']");
but before this do not echo or print anything otherwise it won't redirect to that page.
you can use the data on destination page like this:
$_GET['fname']
example:
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
header('Location:download.php?fname='.$_POST['fname']."&lname=".$_POST['lname']);
view.php
<html>
<body>
<form action="submit.php" method="post">
<input type='text' id="fname" name="fname"/>
<input type='text' id="lname" name="lname"/>
<input type="submit" id="button" value="submit"/>
</form>
</body>
</html>
download.php
<?php
echo "First Name........".$_GET['fname'];
put these three file in same directory and run view.php. you will be ok.
Related
please correct me here.
i have created multi page form where i want to pass data from each pages to final pages and then submit those on email. first one is apply.php, there are many input fields, but i have listed some of those, here when some enters passport number in passport field, i want this to be passed in everypage of the form and print this at couple of places on each page. here getting some issues when passing some of these fields.
this is first page ( apply.php )
<?php
// Start the session
session_start();
?>
<form name="search_form" method="post" onSubmit="return chk();" action="apply2.php">
<input name="passportno" id="passportno" type="text" maxlength="20" placeholder="Enter Passport No." size="43" >
<input name="birthdate" type="date" class="textBoxDashed" size="43" id="birthdate" datepicker="true" datepicker_min="01/01/1900" datepicker_max="21/11/2017" maxlength="10" datepicker_format="DD/MM/YYYY" isdatepicker="true" value="">
<input name="button1" type="submit" value="Continue">
this is apply2.php . here there is some issues, i am not able to find, as you can see below codes, i am able to print date of birth but not able to print passport no ( input from form1 ). Please correct where i am wrong here.
<?php
session_start();
$msg="";
////include("connect.php");
if (isset($_POST['button1']))
{
extract($_POST);
$code=strtolower($_POST['captcha_code']);
$sess=strtolower($_SESSION["code"]);
if ($sess==$code)
{
$appid=time().rand();
$result=mysqli_query($con,"select *from registration where email='$email'");
if (mysqli_fetch_row($result)>0)
{
?>
<script>
alert("This email is already exist");
</script>
<?php
}
else
{
$query="insert into registration values('$appid','$passportno','$birthdate','$email')";
if (mysqli_query($con,$query))
$msg="Data saved Successfully.Please note down the Temporary Application ID $appid";
else
echo "not inserted".mysqli_error($con);
if (!isset($_SESSION["appid"]))
{
$_SESSION["appid"]=$appid;
}
}
}
else
{
?>
<?php
}
}
?>
<form name="OnlineForm" method="post" onsubmit="return OnSubmitForm();" action="apply3.php">
<input name="applid" id="applid" value="<?php echo $_SESSION["appid"];?>">
<input type="hidden" name="birthdate" value="<?php echo $birthdate;?>"><b><?php echo $birthdate;?>
<input name="passportno" type="text" class="textBoxDashed" id="passportno" value="" size="43" maxlength="14" value="<?php echo $passportno;?>">
input name="sc" type="submit" class="btn btn-primary" id="continue" value="Save and Continue" onclick="document.pressed=this.name">
Don't use extract. Also do some checking to see if the data is set. As for not getting the the data try $_POST['passportno'] and if you want to pull the values and put them back into the input boxes simply use <?php echo isset($_POST['passportno'])?$_POST['passportno']:'' ?> to return nothing if it is not defined.
Also you need to do add some protection to your inputs.
You can add protection by using $passportno = mysqli_real_escape_string($con, $passportno);
I'm trying to use POST to get values from a form. I moved the part out to a test page:
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" value="enter something" />
</form>
// following that
<?php
if (isset($_POST["subject"]))
echo $_POST["subject"];
else
echo "input is not set";
?>
The echo is always "input is not set" regardless I set the value of input or not. And the tag "subject" does exist. This confused me. Why can't I get the value?
You should seperate your HTML form from your $_POST processing. This means that you provide the user a page which contains your posted form and then submit of this form is posted to a (different) page.
For example your form is (on form.php):
<form id="form" action="process_form.php" method="post">
<input type="text" name="subject" id="subject" value="" />
<input type="submit" value="Submit">
</form>
Then you create a file process_form.php:
<?php
if (isset($_POST['subject'])) {
echo $_POST['subject'];
}
else {
echo 'input is not set.';
}
?>
How do i stop the submit being shown in the search bar? am i doing something wrong? if i use post method it works fine, but recently i've tried using the get method and it prints both submit and the textbox text in the bar, i tried removing the name="submit" and it didn't do anything when i clicked it. SO how do i stop submit being shown in the address bar?
at the moment it give me $submit=submit, :(
ty all.
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
<?php
if(isset($_GET['submit']))
{
if(!empty($_GET['website']))
{
//do stuffs here
}
else
{
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
Just use:
!empty($_GET)
In your first if statement
So your code should look something like this:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(!empty($_GET)) {
if(!empty($_GET['website'])) {
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
To remove the variables from the URL bar, you need to use the POST method instead of the GET method.
So, this form code should work for you:
<form action="" method="POST">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
You'll also need to alter your PHP code to use $_POST instead of $_GET.
The downside to this is that it won't show the submitted website in the URL bar, meaning users can't bookmark a submitted form. However, this may also be intended.
Alternative Solution
You can remove the name from the submit, and use isset($_GET) to test whether the form was submitted instead.
Here is an example of how this could be done:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(isset($_GET) && count($_GET) > 0) {
if(!empty($_GET['website']))
{
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
Okay, so I programmed this code, referencing various websites. I'm trying to program a signup page for a website. How does the html form connects to the PHP/how do I connect it?
I know that one place I have messed up is the action="" in the form. Different websites have told me to put different things in, from the server name ("localhost"), to "" to the name of the file that the php is in (I want to do it in the same file as the form if that is possible, I tried both that and a separate file). What do I put in there so when submit is clicked, it gives the error messages on the same screen as the form, and when submit is clicked and there are no error messages, it continues? Where do I link the page it continues on to?
Also, tell me if any of my code is deprecated. I've been trying to check everything, but I could of missed something.
<?php
include 'connect.php';
//if submit is clicked
if (isset($_POST['submit'])) {
//then check if all fields are filled
if (!$_POST['username'] | !$_POST['password'] | !$_POST['firstname'] | !$_POST['MI'] | !$_POST['lastname'] | !$_POST['email'] | !$_POST['phonenumber'] | !$_POST['country'] ) {
die('You did not complete all of the required fields'); }
$usernamesquery = mysql_query("SELECT * FROM logins WHERE username='$usernametest'");
if(mysqli_stmt_num_rows($usernamesquery) > 0) {
die('This username is already taken.');
}
}
?>
<form action="????????" method="post">
Username: <input type="text" name="username" maxlength="30"><br>
Password: <input type="password" name="password" maxlength="30"><br>
First Name: <input type="text" name="firstname" maxlength="30"><br>
Middle Initial: <input type="password" name="MI" maxlength="30"><br>
Last Name: <input type="text" name="lastname" maxlength="30"><br>
Email: <input type="password" name="email" maxlength="50"><br>
Phone Number: <input type="text" name="phonenumber" maxlength="11"><br>
Country: <input type="password" name="country" maxlength="40"><br>
<input type="submit">
</form>
If you want it to direct to the same file, then just use:
<form action="" method="POST">
Also there is no $_POST["submit"] since you haven't given your submit button a name.
<input type="submit" name="submit">
Also, does $usernametest actually contain anything? Since you haven't given it a value in your code above.
The "action" is page with some script, that parse the form data and do the login.
For example: the form has action "login.php", that means after you submit the form, the data are sent to "login.php" where you can acces it via $_POST variable. If you have login logic and form in the same file, you don't have to set any action, it's ok if you do it like this
<form action="" method="POST">
More info here
If you want to submit the form to the same page (the URL you are allready on), you can just leave out the action from the <from> tag. Otherwise, you specify the URL (relative or absolute) the form needs to be submitted to.
I want to send a couple of form fields as a POST request to my PHP page, but I can't get it to work. Here is my code:
PHP login.php
<?php
if(!ISSET($_POST["username"]) && !ISSET($_POST["password"])) {
include "login.html";
}
else {
echo "hi";
}
?>
HTML login.html
<form action="login.php" method="post">
<label for="username">Username</label><input type="text" id="username"/>
<label for="password">Password</label>Password<input type="password" id="password"/>
<input type="submit" value="Submit"/>
</form>
Can anyone spot my mistake?
Your inputs do not have names. The id is used for client-side referencing, but it is the (non-unique) name attribute that is used to determine the key for a value when the data is submitted. A form control cannot be successful (i.e. in the form data) without a name.
You haven't included the name attribute in your html input elements. name attribute is used when passing form information to the webserver. id is primarily used for javascript based manipulation.
Username<input type="text" name="username"/>
Password<input type="password" name="password"/>