PHP Includes after submitting a form - php

I'm a bit of a noob when it comes to PHP and forms but I will do my best to explain.
What I am trying to do is create a series of forms that are included in a content container. The user fills out form#1, clicks 'submit' and moves on to fill in form#2 and so on. The data from each form is sent to the next form and is stored in hidden fields and once form#3 is completed the data is all inserted into a MYSQL database.
All of this works fine so far. The problem I am having is that I can't seem to get the submit buttons to work as intended. What I had envisioned is that the user would navigate to 'blah.com/recruit.php?p=form1' and fill out form#1 and then the submit button would take them to 'blah.com/recruit.php?p=form2' and so forth.
<form id="form" action="recruit.php?p=form2" method="post">
This does not work but I don't understand why. I've looked around the internet and I've found a few forum topics that discuss similar issues but none of them actually go into much detail about the solution or why this approach won't work.
Can anyone explain to me what it is I am doing wrong please? I have a feeling it is stupidly obvious but I can't put my finger on it.
Many thanks,
Splatgore

Some working sample code to do what I think you're trying to achieve:
<?php
$p = intval($_GET['p']);
if ($p == 0)
{
$p = 1; // default to first form
}
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
{
// get / set all forms values here since subsequent forms will be
// posting the previous forms' data via hidden fields
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$tel_number = $_POST['tel_number'];
if ($p == 3)
{
// all forms done, insert to MySQL here
}
if ($p == 2)
{
// form 2's validation
$p = 3; // set so that the form that follows will now show the
// third form
}
if ($p == 1)
{
// form 1's validation
$p = 2; // set so that the form that follows will now show the
// second form
}
}
?>
<form id="form" action="recruit.php?p=<?php echo $p; ?>" method="post">
<h2>Form #<?php echo $p; ?></h2>
<?php
if ($p == 1)
{
?>
<!-- form 1's fields // -->
<p><label for="first_name">First Name:</label>
<input type="text" name="first_name" id="first_name" size="40" value="<?php echo $first_name; ?>" /></p>
<p><label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="last_name" size="40" value="<?php echo $last_name; ?>" /></p>
<?php
}
if ($p == 2)
{
?>
<!-- form 2's fields // -->
<p><label for="tel_number">Tel Number:</label>
<input type="text" name="tel_number" id="tel_number" size="25" value="<?php echo $tel_number; ?>" /></p>
<p><input type="hidden" name="first_name" value="<?php echo $first_name; ?>" />
<input type="hidden" name="last_name" value="<?php echo $last_name; ?>" /></p>
<?php
}
if ($p == 3)
{
?>
<!-- form 3's fields // -->
<div>
<input type="hidden" name="first_name" value="<?php echo $first_name; ?>" />
<input type="hidden" name="last_name" value="<?php echo $last_name; ?>" />
<input type="hidden" name="tel_number" value="<?php echo $tel_number; ?>" />
</div>
<?php
}
?>
<p><input type="submit" value="Submit"></p>
</form>

It looks somehow you have nested forms.
This is not possible in HTML.
If you are going with your approach, just use always the same submit button which leads to the same php script.
In the php script check which hidden fields are already set to see how far the process is.
A better approach IMO would be tough to store the data in a session var and always lead to another form page.

Related

Problem: Part of the PHP exercise does not run correctly

I created a section for editing. When I edit the information and click the save button, the information is not saved and the header section does not display completely.
<?php
if (isset($_POST['submit_btn']))
{
$id = $_POST['id'];
$fn = trim($_POST['name']);
$ln = trim($_POST['lastname']);
$age = trim($_POST['age']);
$q = "UPDATE `users` SET `fn` = '$fn',
`ln` = '$ln',
`age` = '$age'
WHERE id = '$id'";
mysqli_query($dbconnect,$q);
if (mysqli_affected_rows($dbconnect) > 0)
redirect("?msg=ok&id=**$id**");
else
redirect("?msg=error&id=**$id**");
}
else
echo ("Not In If(isset)");
?>
<form action="" method="post">
<label for="name">FirstName:</label>
<input type="text" name="name" id="name" value="<?php echo $row['fn']?>">
<br>
<label for="lastname">LastName:</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $row['ln']?>">
<br>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $row['age']?>">
<br>
<input type="submit" name="submit_btn" value="Save">
<a href="index2.php">
Back
</a>
</form>
</body>
Bold sections do not work here.
Below is a picture of the result:
In the link that I specified, after clicking on save the ID will not be displayed and all the information filled in the forms will be lost.
Sorry if the result is styleless and boring and I just created this page to practice php😁
Thank you for being responsive🙏🙏🙏
You are mistaking a POST request with a GET request.
Part, which appears in the URL is sent to the webserver in GET request.
Your form is submitting POST request to the webserver, logic in the code does the same, but you are trying to display information from url (GET).
Please check the examples in php.net:
POST variables: https://www.php.net/manual/en/reserved.variables.post.php
GET variables: https://www.php.net/manual/en/reserved.variables.get.php
You can take an example with GET request variable below, however, be careful with trusting the "end client" and always prepare your statements, which you send to your database to execute queries.
if (isset($_GET['submit']))
{
$number = $_GET['number'];
echo $number
? "Number which was submitted: $number <br>"
: 'Number is not set';
} else {
echo 'Form has not been yet submitted';
}
?>
<form action="" method="get">
<input type="number" name="number" placeholder="Number">
<input type="submit" name="submit" value="Save">
</form>

php multipage input value session variable print

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);

PHP simple form not posting

I've been tearing my hair out trying to figure out why the isset($_POST['Submit']) is not executing with my form. The data from the form is just not passing into the php code. Basically the code does not seem to be recognizing something like $ffname = $_POST["ffname"];
<?php
$ffname = $flname = $femail = $fcemail = $fpass = $fcpass = "";
if(isset($_POST['ffname'])){
$ffname = $_POST["ffname"];
$flname = $_POST["flname"];
$femail = $_POST["femail"];
$fcemail = $_POST["fcemail"];
$fpass = $_POST["fpass"];
$fcpass = $_POST["fcpass"];
echo "<p>Hello World<p>";
$con = mysqli_connect("localhost", "root", "") or die(mysqli_error());
mysqli_select_db($con, "userdata") or die(mysqli_error($con));
mysqli_query($con,"INSERT INTO tbluser (fname, lname, email, pass) VALUES('$ffname', '$flname', '$femail', '$fpass')")
or die (mysqli_error($con));
}
?>
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
The other answer by #DerVO is correct. But there seems to be something else at play, since you say it still doesn't work.
A comment became too long, so I've built a full answer here.
Step 1:
Add a name to your input:
<input type="submit" name="Submit" value="submit">
However, relying on the submit in your $_POST is not the best plan. So I suggest watching a different form field - for example, ffname:
Step 2:
Improve your watch, using a different field:
if ( isset( $_POST['ffname'] ) ) {
// do your work
}
Lastly, you may be munging your form action attribute.
Step 3:
In order to keep things simple, if the form is supposed to submit to the same page, you can simply omit the form action.
<form method="post">
Betweeen these three items, the form will work, unless you have some problem with your server.
Step 4:
Clean up your form formatting. You've got odd spacing which is problematic. In an html element, the property="value" code needs to be without spaces, but spaces between properties. Example:
<!-- Your version -->
<input type = "text"name = "ffname"id = "ffname"value="<?php echo $ffname;?>"><br>
<!-- Clean / correct version -->
<input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Here's a "clean" version of your whole form:
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
You need to give your input submit a name:
<input type="submit" name="Submit" value="Submit">
You have pass name of element in $_POST
try put name attribute in input submit
<input type = "submit" name="Submit" value = "1">

Auto-Increment Form Input Names With $_SESSION

I want to use PHP $_SESSION to access user form-inputted data. The flow looks like this:
STEP 1
User fills out form.
<form method="post" action="review.php">
<input type="text" name="length">
<input type="text" name="width">
<input typ="text" name="quantity">
<input type="submit" value="submit">
</form>
STEP 2
Form data is submitted and 'review.php' converts from POST to SESSION. User reviews form details and adds item to cart.
STEP 3
Item is added to PayPal Shopping Cart. User decides he wants to add another item so user clicks on "continue shopping" and returns to STEP 1.
STEP 1
User fills out form details. Except this time, I want the input name to auto-increment.
For example:
<input type="text" name="length2">
<input type="text" name="width2">
<input type="text" name="quantity2">
And I want it to auto increment however many number of times the user adds new items to the cart.
<input type="text" name="length3">
<input type="text" name="length4">
<input type="text" name="etc">
Usual flow continues... STEP 2 ---> STEP 3
After successful payment in STEP 3, user will be redirected to a new page.
STEP 4(newPage)
All of the purchased items' details will be displayed on this page in order and with the same group. For example, (length, width, quantity) then (length2, width2, quantity2) and so on and so forth. They will each be displayed in different forms.
Those forms will also include <input type="file"> so that the user can upload a file for each form. Each form represents a purchased item.
Once the user uploads files to each corresponding form, they will be submitted to a database.
QUESTION
How can I make the input names auto increment preferably with PHP,
and of course whilst using $_SESSION? I would like the name to
return to its original name after the user's session. So let's say
that same user wanted to order the next day and he ordered 5 items,
the input names would span to 5 (length, length2,..., length5).
How can I display all of the user inputted data in each respective
form(see STEP 4)?
I know I have to do all of the back-end stuff on PHP($_SESSION, forms, etc) but if someone has a jQuery solution for the auto increment and showing all of the data in the end I'm all for it.
Here's What I Was Working With
Tried to use this to display the data with the incremented names but couldn't figure out a solution.
<?php
session_start();
?>
<form>
<?php
$size = $_SESSION['size'];
$variables = array("$size");
$i = 1;
foreach ($variables as $var ) {
$name = "txt".$i;
echo "<input type='text' name='".$name."' value='".$var."' />";
$i++;
}
?>
</form>
Some of you might bring up PayPal IPN. I created an IPN listener as well but it doesn't get called via sandbox. A user on here posted that his IPN listener didn't work on sandbox either but when he went live via paypal.com it did work. Could be the case with mine too, but I want to go ahead and start selling so I need a different approach. Even if my IPN listener does work via paypal.com, I can't risk going live with it without being 100% sure. So if I can use this method for now until I can confirm that my listener works I will do it.
As always I am very grateful for your guys' help.
I actually figured this out a while back. Here's the code I am using:
<?php
if(isset($_POST['submit'])){
$_SESSION['invoice'] = $_POST['invoice'];
$invoice_no = $_SESSION['invoice'];
$decal = array(
'length' => $_POST['os0'],
'width' => $_POST['os1'],
'color' => $_POST['os2'],
'quantity' => $_POST['os3'],
'price' => $_POST['price'],
'submit' => $_POST['submit']
);
$_SESSION['order'][] = $decal;
$i = 0;
}
if(isset($_SESSION['order'])){
foreach($_SESSION['order'] as $sav) {
$i++;
?>
<input type="hidden" name="on0_<?php echo $i; ?>" value="Length">
<div class="decalName">
<label>Custom Decal <?php echo $i; ?></label>
</div>
<div class="label-input">
<label>Length</label><input type="text" name="os0_<?php echo $i; ?>" id="length" size="2" class="num" value="<?php echo $sav['length']; ?>" readonly>
</div>
<input type="hidden" name="on1_<?php echo $i; ?>" value="Width">
<div class="label-input"><label>Width</label><input type="text" name="os1_<?php echo $i; ?>" id="width" size="2" class="num" value="<?php echo $sav['width']; ?>" readonly>
</div>
<input type="hidden" name="on2_<?php echo $i; ?>" value="Color">
<div class="label-input">
<label>Color</label><input type="text" name="os2_<?php echo $i; ?>" value="<?php echo $sav['color']; ?>" readonly>
</div>
<div class="label-input">
<label>Quantity</label><input type="text" name="os3_<?php echo $i; ?>" size="2" class="num" id="quantity" value="<?php echo $sav['quantity']; ?>" readonly>
</div>
<input type="hidden" name="on3_<?php echo $i; ?>" value="quantity">
<input type="hidden" class="num" value="0.20">
<div class="label-input">
<label>Price $:</label><input type="text" class="tot" name="price" value="<?php echo $sav['price']; ?>" readonly>
</div><!--end label-input-->
<?php
}
?>

Add value on textbox upon clicking submit

Is it possible to create multiple html text inputs on my
<form method="POST" id="2">
using:
<input type="text" value="">
with an initial value of none and fill it with a value after clicking submit from a previous
<form method="POST" id="1">
Can anyone help me with this?
Thanks in advance! By the way, I'm using PHP.
This question is a bit vague and tagged incorrectly.
PHP is executed server side. The only way to modify your form is by using Javascript.
The answer to your "Is it possible" question is simply yes.
<form name="myform" action="handle-data.php">
Input: <input type='text' name='name' value='none' />
Submit
</form>
<script type="text/javascript">
function submitform()
{
// Do things here
document.myform.submit();
}
</script>
#zerey: I coded up this commented contrived example of what I think you might have been hoping to achieve using PHP only and I've combined it all into one form --
<?php
if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST")
{
$foo = intval($_POST['foo']);
$bar = $_POST['bar'];
if (count($bar) > 0)
{
// the additional inputs that were created now contains data
// and furter validation can take place here
}
}
if (!isset($foo))
{
$foo = 0; // default the # of inputs field to 0
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<div>
<label for="foo"># inputs:</label>
<input type="text" name="foo" size="3" maxlength="1" value="<?php echo $foo; ?>">
<input type="submit" value="Submit">
</div>
<?php
// a check to limit the amount of additional inputs that can be
// created
if ($foo < 10 && $foo > 0)
{
?>
<div>
<?php
// loop to output the # of inputs the user previously entered
for ($i = 0; $i < $foo; $i++)
{
?>
<br>
<label for="bar[<?php echo $i; ?>]"><?php echo $i + 1; ?></label>
<input type="text" name="bar[<?php echo $i; ?>]" id="bar[<?php echo $i; ?>]" value="<?php echo $bar[$i]; ?>">
<?php
}
?>
</div>
<?php
}
?>
</form>
Do they have to be separate forms?
You could use something like the jQuery Form Wizard Plugin to split up the form in the UI and use the step_shown event to populate the fields you need.

Categories