This question already has answers here:
Submit HTML form on self page
(5 answers)
Closed 2 years ago.
I have a page for people to submit their information /collect.php but when submitting it keeps going to the root. Is it possible to submit the form and make it go to the same collect?
Here is the code I have so far:
<?php if (!empty($_POST)):
$firstname = htmlentities($_POST['firstname']);
$lastname = htmlentities($_POST['lastname']);
$email = htmlentities($_POST['email']);
$phone = htmlentities($_POST['phone']);
echo $firstname."<br/>";
echo $lastname."<br/>";
echo $email."<br/>";
echo $phone."<br/>";
else: ?>
<form action="." method="post">
<input type="text" name="firstname" placeholder="First name"><br/>
<input type="text" name="lastname" placeholder="Last name"><br/>
<input type="email" name="eamil" placeholder="Email address"><br/>
<input type="phone" name="number" placeholder="Phone number"><br/>
<input type="submit">
</form>
<?php endif; ?>
You're almost there, to submit to the same page you can use the $_SERVER["PHP_SELF"] which is a super global variable that returns the filename of the currently executing script.
You'll also want to be careful with your input names, email is misspelled and the form for the phone number is named number while the post is labelled phone. Also, the correct the correct HTML5 for a phone number is tel.
<?php if (!empty($_POST)):
$firstname = htmlentities($_POST['firstname']);
$lastname = htmlentities($_POST['lastname']);
$email = htmlentities($_POST['email']);
$phone = htmlentities($_POST['phone']);
echo $firstname."<br/>";
echo $lastname."<br/>";
echo $email."<br/>";
echo $phone."<br/>";
else: ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="text" name="firstname" placeholder="First name"><br/>
<input type="text" name="lastname" placeholder="Last name"><br/>
<input type="email" name="email" placeholder="Email address"><br/>
<input type="tel" pattern="([0-9]{3}) [0-9]{3}-[0-9]{4}" name="phone" placeholder="(123) 456-7890" required><br/>
<input type="submit">
</form>
<?php endif; ?>
Related
I have a form called addcustomer.php . it's contain firsname, lastname, mobile fields.i have also 2 button on this form. onr button for saving data and the others for sending sms to customers.
i want to when i click second button 3 data fields passed to another form called smsinfo.php.
now action for saving data works good but when direct to smsinfo . there is no data on this form .
<form method ="POST" name = "custform" action="">
<div class="cell_2_2"><label class="lblfields">Firstname:</label> <input type="text" name="firstname" autocomplete="off"/></div>
<div class="cell_3_2"><label class="lblfields">Lastname :</label> <input type="text" name="lastname" autocomplete="off"/></div>
<div class="cell_5_2"><label class="lblfields">Mobile :</label> <input type="text" name="mobile" autocomplete="off"/></div>
<input type="submit" name="submit" value="Save"/>
<input type="submit" name="sendsms" value="Sms"/>
if (isset($_POST['submit']))
{
Saving code here
} else if (isset($_POST['sendsms'])) {
header("Location: smsinfo.php");
}
here code of smsinfo.php:
<?php
$Fname = $_REQUEST['firstname'];
$Lname = $_REQUEST['lastname'];
$Mob = $_REQUEST['mobile'];
?>
<html>
<body>
<form action="sendingsms.php" method="POST">
<input style="width: 100px;" name="firstname" type="text" value="<?php echo $firstname; ?>"/>
<input style="width: 100px;" name="lastname" type="text" value="<?php echo $lastname; ?>"/>
<input style="width: 100px;" name="mobile" type="text" value="<?php echo $mobile; ?>"/>
</form>
</body>
</html>
thanks all and sorry for my poor english
<form id="superheroForm" action="submit.php" method="post">
<p><i>Please complete the form. Mandatory fields are marked with a </i><em>*</em></p>
<fieldset>
<legend>Contact Details</legend><br>
<label for="Name">Name <em>*</em></label>
<input id="name" name="name" placeholder="Jane " autofocus required><br>
<label for="telephone">Telephone <em>*</em></label>
<input id="telephone" placeholder="(xxx) xxx-xxxx" title="must be in the following format (xxx)-xxx-xxxx"
pattern=[0-9]{3}-[0-9]{3}-[0-9]{4} required><br>
<label for="email">Email <em>*</em></label>
<input id="email" type="email" required><br><br>
</fieldset>
</form>
I have another file named submit.php. Once the user clicks on submit application button, I am supposed to get the php response something like this:
Thanks for submitting your form
Name:
Telephone:
So far I have tried this directly in a new file named submit.php but it doesn't work at all: This is my php code:
<html>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</html>
</body>
Update:Solution: Sorry I did not install PHP the right way.
your php code should contain something like this
<?php
$name = $_POST['name'];
//then just echo them
echo $name;
?>
Change your superhero form like this..
<form id="superheroForm" action="submit.php" method="post">
<p><i>Please complete the form. Mandatory fields are marked with a </i><em>*</em></p>
<fieldset>
<legend>Contact Details</legend><br>
<label for="Name">Name <em>*</em></label>
<input type="text" id="name" name="name" placeholder="Jane " autofocus required> <br>
<label for="telephone">Telephone <em>*</em></label>
<input id="telephone" placeholder="(xxx) xxx-xxxx" name="tel" required><br>
<label for="email">Email <em>*</em></label>
<input id="email" type="email" required><br><br>
</fieldset>
</form>
and Your submit.php is like this
<?php
if(isset($_POST['name'])){
echo "Thanks for submitting your form ";
echo "Name:".$_POST['name'];
echo "Telephone:".$_POST['tel'];
}
?>
This is it.
I am new to php and just cant figure how to get the data from a a html textbox and then echo the var.
I am making a registration page, I do everything else but this simple part.
I have a preset value="hi" just for testing if the var populates.
Fyi in the future it will be done after i click a register button. just need to get this.
Thanks all
<input name="fName" id="fName" type="text" value="hi" />
and here is the php which i try to read the data into if the echo is test to check if it populates
<?php
$fName = $_POST['fName'];
//$fName = $_GET['fName'];
echo $fName;
?>
<input type="text" placeholder="NAME" name="name" value="<?php if(isset($_POST['name'])){ echo $_POST['name']; } ?>" />
<input type="text" name="phone" placeholder="PHONE" value="<?php if(isset($_POST['phone'])){ echo $_POST['phone']; } ?>" />
This should work for you:
You have to make a form and submit it! After that you can use the variable$_POST['fName']
<?php
if (isset($_POST['fName'])) //if you also want to check if it is empty use !empty($_POST['fName'])
echo $_POST['fName'];
?>
<form action="" method="post">
<input name="fName" id="fName" type="text" value="hi" />
<input type="submit" name="submit" value="submit!" />
</form>
This question already has an answer here:
Submission form won't stay on same page
(1 answer)
Closed 9 years ago.
Okay so, I've posted this already but still haven't found a solution. I can't seem make my form stay on the same page and I've basically tried EVERYTHING I could possibly think of.
<?php include("inc\incfiles\header.inc.php"); ?>
<?php
$reg = #$_POST['reg'];
//declaring variables to prevent errors
$fn = $ln = $un = $em = $em2 = $pswd = $pswd2 = $d = $u_check = "";
/*$fn = ""; //First Name
$ln = ""; //Last Name
$un = ""; //Username
$em = ""; //Email
$em2 = ""; //Email 2
$pawd = ""; //:Password
$pawd2 = ""; //Password 2
$d = ""; //Sign up Date
$u_check = ""; //Check if username exists*/
//registration form
$fn = mysql_real_escape_string(#$_POST['fname']);
$ln = mysql_real_escape_string(#$_POST['lname']);
$un = mysql_real_escape_string(#$_POST['username']);
$em = mysql_real_escape_string(#$_POST['email']);
$em2 = mysql_real_escape_string(#$_POST['email2']);
$pswd = mysql_real_escape_string(#$_POST['password']);
$pswd2 = mysql_real_escape_string(#$_POST['password2']);
$d = date("Y-m-d"); //Year - Month - Day
if ($reg)
{
//check all of the fields have been filled in
if ($fn && $ln && $un && $em && $em2 && $pswd && $pswd2) {
}
else{
echo "please fill in all fields...";
}
}
?>
<table class="homepageTable">
<tr>
<td width="60%" valign="top">
<center><h2>Join the community today!</h2></center>
<center><img src="images/photo.png" width="500"></center>
<form>
</td>
<td width="40%" valign="top">
<h2>Get started below...</h2>
<form action="#" method="post">
<input type="text" size="25" name="firstname" placeholder="First Name" value="<?php echo $fn; ?>"/>
<input type="text" size="25" name="lastname" placeholder="Last Name" value="<?php echo $ln; ?>"/>
<input type="text" size="25" name="username" placeholder="Username" value="<?php echo $un; ?>"/>
<input type="text" size="25" name="email" placeholder="Email" value="<?php echo $em; ?>">
<input type="text" size="25" name="email2" placeholder="Repeat Email" value="<?php echo $em2; ?>"/>
<input type="password" size="32" name="password" placeholder="Password"/>
<input type="password" size="32" name="password2" placeholder="Repeat Password"/><br />
<input type="submit" name="reg" value="Sign Up!"/>
</form>
</td>
</tr>
</table>
My biggest problem is with the:
<form action="#" method="post">
<input type="text" size="25" name="firstname" placeholder="First Name" value="<?php echo $fn; ?>"/>
<input type="text" size="25" name="lastname" placeholder="Last Name" value="<?php echo $ln; ?>"/>
<input type="text" size="25" name="username" placeholder="Username" value="<?php echo $un; ?>"/>
<input type="text" size="25" name="email" placeholder="Email" value="<?php echo $em; ?>">
<input type="text" size="25" name="email2" placeholder="Repeat Email" value="<?php echo $em2; ?>"/>
<input type="password" size="32" name="password" placeholder="Password"/>
<input type="password" size="32" name="password2" placeholder="Repeat Password"/><br />
<input type="submit" name="reg" value="Sign Up!"/>
</form>
I want to be able to press the submit button and have it stay on the same page.
I've tried leaving the blank I've tried a few other suggestions but I keep coming up with nothing. I've been trying to figure it out for 2 days now and it just won't budge. When pressing the submit button on my site in xampp, it just takes me to another page that says OBJECT NOT FOUND...etc.
If anyone can help, it would be greatly appreciated! I really don't want to have to start all over with my coding just because of one mistake.
Header.inc.php
<?php
include("inc/scripts/mysql_connect.inc.php");
?>
<html>
<head>
<link href="css\main.css" rel="stylesheet" type="text/css">
<title>website</title>
</head>
<body>
<div class="headerMenu">
<div id="wrapper">
<div class="logo">
<img src="images/Logo.png">
</div>
<div class="search_box">
<form method="GET" action="search.php" id="search">
<input name="q" type="text" size="60" placeholder="Search..."
</form>
</div>
<div id="menu">
Home
About
Sign Up
Log in
</div>
</div>
<br />
<br />
<br />
<br />
TRY USING $_SERVER['PHP_SELF']
http://php.net/manual/en/reserved.variables.server.php
By staying on the same page , I think you mean you want to submit the page , refresh & send data. if you mean that , so you may use this as your tag :
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" >
However , if you want your page not to be refreshed , you need to work with Jquery , JavaScript etc.
Your problem is this line in your inc\incfiles\header.inc.php
<input name="q" type="text" size="60" placeholder="Search..."
You are missing the closing bracket -
<input name="q" type="text" size="60" placeholder="Search..." />
^^
because of that, the closing form tag on the next line is not beinging parsed, so your form in index.php is being nestled inside the <form method="GET" action="search.php" id="search">
Instead of form action="#" write form action="" Notice there are 2 ticks with nothing in between.
I am trying to set up an application form on my site where there is a main large application form on one page (parent.php)and a smaller form on another page (child2.php) that when users fill out some of their details and submit that smaller form, they are taken to the larger form and the details they have entered already appear in the corresponding textbox on the larger form along side some extra boxes for them to fill out(if that makes sense!)
I can get it to work in that the textboxes on the 2nd page display the values of the matching textboxes on the first page, but only when a value is set. As users can either access the main application form through the smaller form OR by directly accessing it, I need to have it so that if the value is set, the set value is displayed and will also be the value entered into the database OR if the value is not pre-set from the smaller form, the user can enter in their info to the main form and this is what's sent to the DB. I think I might need to use ifisset and have tried to do so but am getting nowhere.
Apologies for messy code and the set up of the textboxes as they are just for testing this out and I am still getting to grips with all this and would be grateful if anyone could help me/let me know if I'm on the right track/totally off. Thanks in advance!
Page 1 (parent.php)
<form action="child2.php" method="post" class="validate">
<div>
<input class="tb" type="text" name="fName" placeholder="first name" id="fName" value="<?php $fName ?>" required/><br/>
<br/>
<input class="tb" type="text" name="sName" placeholder="surname" id="sName" value="<?php $sName ?>" required/><br/>
<br/>
<input class="tb" type="email" name="email" required placeholder="email address" id="email" value="<?php $email ?>" required/>
<br/>
<input class="tb" type="address" name="address" placeholder="address" value="<?php $address ?>" id="address" />
<br/>
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
Page 2 (child2.php)
<?php
function renderForm($fName, $sName, $email, $address){
?>
<form action="" method="post" class="validate">
<label class="label">first name</label><input class="tb" type="text" id="fName" name="fName" value="<?php if (isset($fName)) { echo $fName = $_REQUEST['fName'];} else { echo "first name"; }?>"/>
</br>
<label class="label">surname</label><input class="tb" type="text" id="sName" name="sName" value="<?php if (isset($sName)) { echo $sName = $_REQUEST['sName'];} else { echo "surname"; }?>"/>
</br>
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php if (isset($email)) { echo $email = $_REQUEST['email'];} else { echo "email"; }?>"/>
</br>
<label class="label">address</label><input class="tb" type="text" id="address" name="address" value="<?php if (isset($address)) { echo $address = $_REQUEST['address'];} else { echo "address"; }?>"/>
</br>
What you're looking for are $_SESSION variables, which stay active until the user closes the browser or until the Session expires (I think the default PHP config time is 24 minutes..?). This is a lot more efficient than storing them in a database for temporal purposes.
So you can set the variables with $_SESSION['fName'] = $_POST['fName']; etc. and then call them later with echo $_SESSION['fName']; etc.
To utilize session variables you will need <?php session_start(); ?> at the beginning of your pages (before any HTML is used)
As suggested by khanahk, using SESSION variables will do your job. The problem with the solution you are thinking is that its fine only as long as you are passing values from one page to some other page, that too after so much mess.
So, you should instead, you should use something like this
session_start();
$email = $_SESSION['email'];
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php echo $email; ?>"/>