Form submitting but I don't know how or why? - php

I've made a normal HTML form which when submitted, sends information to another page and that page processes the data and adds it to the database. But for some reason, if I refresh the 1st page which contains the form, an entry is being made into the database even though I can't see any reason for it to do so.
This is the code for the form:
<?php $owner = $_GET['user']; ?>
<h2 class="dotted"><?php _e('Add feedback for user','appthemes'); echo " - ".$owner;?></h2>
<form id='feedback' action='http://www.example.co.uk/add-feedback-response/?user=<?php echo $owner; ?>' method='POST' accept-charset='UTF-8'>
<fieldset >
<b>Your Username:</b> <?php echo $current_user->user_login; ?><br/><br/>
<b>Rating: </b>
<input type="radio" name="rating" value="positive" /> Positive
<input type="radio" name="rating" value="neutral" /> Neutral
<input type="radio" name="rating" value="negative" /> Negative<br/><br/>
<label for='item' ><b>Item bought:</b></label>
<input type="text" name='item' id='item' maxlength="500"/><br/><br/>
<label for='comment' ><b>Comment:</b></label>
<textarea name='comment' id='comment' maxlength="2500"></textarea><br/><br/>
<input type="submit" id="submit-button" value="Submit Feedback" />
</fieldset>
</form>
Is there something I'm missing? Thanks for any help

It is probably because you're trying to submit form again. Is browser alerting you that you're sending data again?
to restrict this, use
header("Location: ".$_SERVER['HTTP_REFERER']);
in proccessing form

If you refresh the form after it has submitted once, the form will be submitted again. (the browser would have warned you, "Are you sure you want to submit the page again" - didn't it?) Refresh is actually "repeat the last request".
If you think such a scenario is possible in your webapp, you will have to handle duplicate requests.

Related

Form submit didnot working properly and return data

I have a form and here is the code
<form method="post" id="dvs-upload-booking">
<input type="hidden" name="tcs_bulk_booking" value="true">
<input disabled="" style="text-align: center;" type="text" id="1" name="1" value="1" required="">
<input type="text" id="name_1" name="order[1][name]" value="Peter " required="">
<input type="submit" value="Submit" class="dvs-btn-upload">
</form>
This is my php code which I try to run when someone click submit
<?php
if (!empty($_POST["tcs_bulk_booking"])) {
echo "Yes, Data is set";
} else {
echo "No, Data is not set";
}
?>
But when I click Submit button it still show "No, Data is not set" despite it need to show "Yes, Data is set".
Please tell me what I did wrong
I have tried your code and it outputs "Yes, Data is set" when the button is clicked. Try clearing your browser cache or use the incognito mode to test. Or just close and reopen your browser

Why are the nested forms processed with $_SERVER['PHP_SELF'] (same html file) not displaying/processing correctly?

I'm trying to get user input in a progressive sequence that leads to that input being sent by email. Sending by email is a whole other issue that I haven't tackled yet so not really worried about that.
The part I am having difficulty with is once the user gets to the "Send Email?" (Yes/No) radio buttons, the input from that question is not processed correctly.
I've gotten further with this by using a separate php file as the form action but still get errors related to emailName, emailAddress, and emailMsg not existing ("Notice: Undefined index...").
Furthermore, I still need to be able to use the $_POST[athletes] array further down but I'm guessing it's outside of the variable scope at that point.
So to bring that all together, I'm really asking a few questions:
1) How can I get all of the forms to work together in the same file?
2) When the program actually goes past the "Send Email?" radio buttons when I use a separate php file as the form action, why am I getting undefined index errors?
3) Why do I get an error when I try to use the athletes[] array further down in the code? Should I somehow be passing the array values to that part of the code?
The exact steps the user would take to get to the issue is:
Select 1 or more athlete checkboxes and click the 'Display Selection(s)' button.
Select 'Yes' for "Send Email?" and click the 'Submit' button.
Restarts the code for some reason.
Any help would be greatly appreciated. Also, this is my first post so sorry if I asked the question incorrectly or not according to site etiquette.
I also apologize for the long code fragment but I'm not sure what parts might be causing this to be incorrect.
<b><h1><center>Athelete Selection Screen</center></h1></b>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<fieldset>
<legend>Athletes Available: </legend>
<input type="checkbox" id="student1"
name="athletes[]" value="Student1 Test">
<label for="student1">Student1 Test</label><br/>
<font color="grey">Football - Running back</font><br/>
<p>
<input type="checkbox" id="student2"
name="athletes[]" value="Student2 Test">
<label for="student1">Student2 Test</label><br/>
<font color="grey">Soccer - Left Forward</font><br/>
</p>
<p>
<input type="checkbox" id="student3"
name="athletes[]" value="Student3 Test">
<label for="student1">Student3 Test</label><br/>
<font color="grey">Baseball - Pitcher/Left Outfield</font><br/>
</p>
</fieldset>
<p>
<?php echo("\t\t\t\t\t"); ?><button type="submit" name="submit" value="submit">Display Selection(s)</button>
</p>
</form>
<fieldset>
<legend>Athletes You Selected: </legend>
<?php
if (!empty($_POST['athletes']))
{
echo "<ul>";
foreach($_POST['athletes'] as $value)
{
echo "<li>$value</li>";
}
echo "</ul>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<fieldset>
<legend>Send Email? </legend>
<input type="radio" id="Yes"
name="radioSendMsg[]" value="Yes">
<label for="student1">Yes</label>
<p>
<input type="radio" id="No"
name="radioSendMsg[]" value="No">
<label for="student1">No</label><br/>
</p>
<button type="submit" name="submitRadio" value="submit">Submit</button>
</p>
</form>
<?php
if (!empty($_POST['radioSendMsg']))
{
foreach($_POST['radioSendMsg'] as $radioMsg)
{
if($radioMsg == "Yes")
{
echo "\tPlease enter information regarding the email to be sent: ";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<label for="emailName"> Name: </label><br/>
<input type="text" size="25" id="emailName" name="emailName" />
</p>
<p>
<label for="emailAddress">E-mail Address: </label></br>
<input type="text" size="25" id="emailAddress" name="emailAddress" />
</p>
<p>
<textarea id="emailMsg" name="emailMsg" cols="30" rows="5"></textarea>
</p>
<button type="submit" name="emailSubmit" value="send">Send Message</button>
</form>
<?php
$msg = "Name: ".$_POST['emailName']."\n";
$msg.= "E-Mail: ".$_POST['emailAddress']."\n";
$msg.= "Message: ".$_POST['emailMsg']."\n";
$msg.= "<ul>";
foreach($_POST['athletes'] as $value)
{
$msg.= "<li>$value</li>\n";
}
$msg.= "</ul>";
$emailRecipient = "sjzerbib#gmail.com";
$emailSubject = "Athlete Selection Submission";
$emailHeaders = "From: Sebastien\n"."Reply-To: ".$_POST['emailAddress'];
mail($emailRecipient,$emailSubject,$msg,$emailHeaders);
echo "Message sent: \n".$msg;
}
else
{
?> <p /> <?php
echo "\n\nNo email will be sent for your last athlete selection.";
?>
<br/>Please click here
to return to the Athlete selection screen.
<?php
}
}
}
}
When you submit a form, only those controls contained within that form are included. The exception is successful controls that have the form attribute set to the id value of the form that was submitted.
So, given you had something like:
<form id="form-1" method="post">
<input type="text" name="first-input" />
</form>
<input type="text" name="second-input" />
The only value to be submitted would be that of first-input. If you add the form attribute to second-input:
<input type="text" name="second-input" form="form-1" />
Then the submission of the form would include both values. Unfortunately, the form attribute is not fully supported (IE and Edge have no support).
Of course, your markup is invalid, so that's a moot point. For starters, you cannot nest a form within a form. How a browser responds to markup that violates that rule is up to it's vendor, but in my experience is somewhat unpredictable. You're also using deprecated tags (<font> and <center> are no longer valid) and nesting elements incorrectly (<h1> is a block level element, whereas <b> is inline).
If you're doing a full submit each time (so the page gets submitted to itself and then reloads), then just use some sort of conditional to only render the dependent controls if the preceding form submissions were successful:
<?php
$canDoNextStep = !empty($_POST['input-1']);
?>
<form id="form-1" method="post">
<input type="text" name="first-input" />
<?php if(canDoNextStep): ?>
<input type="text" name="second-input" />
<?php endif; ?>
</form>
Lastly, whitespace is (mostly) ignored when your browser parses and displays your HTML, so you can lose the \t and \n values in your strings, unless you're concerned about how your markup looks if someone chooses to view source when using your form.

Redirecting to the other page after submitting radio button option

I have a problem with my form. I need it to redirect user to different pages basing on which radio button was selected. User has to choose one of two options and click next, and according to his choice, page should redirect him to other page.
Here is the code as it looks for now
<fieldset>
<legend>Select option</legend>
<center>
<form method="post" action="">
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio2" value="Firma"/>Company
</br>
<input type = "submit", class = "buttonStyle2", value=""/>
</form>
</center>
</fieldset>
and then php code
if(isset($_POST['Company'])
header("Location: http://myaddress.com/company.php");
Big thanks in advance for your help
Here is one way to achieve this.
Sidenote: Make sure you're not outputting before header. Consult this page on Stack about possible Headers already sent..., should this occur and making sure error reporting is set/on.
Otherwise, PHP will fail silently.
if(isset($_POST['radio1']) && ($_POST['radio1']) == "Osoba fizyczna"){
header("Location: http://www.example.com/non_company.php");
}
elseif(isset($_POST['radio1']) && ($_POST['radio1']) == "Firma"){
header("Location: http://www.example.com/company.php");
}
else{
header("Location: http://www.example.com/redirect_to_home.php");
}
Nota: The else would be if the person did not make a choice and simply clicked on submit without making a selection.
while using radio buttons of the same group name in your form:
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio1" value="Firma"/>Company
Note about
<input type = "submit", class = "buttonStyle2", value=""/>
remove the commas
<input type = "submit" class = "buttonStyle2" value=""/>
Since HTML source in FF will reveal No space between attributes in red/as an error.
<fieldset>
<legend>Select option</legend>
<center>
<form method="post" action="">
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio1" value="Firma"/>Company
</br>
<input type = "submit" class = "buttonStyle2" value=""/>
</form>
</center>
</fieldset>
and
if ( isset($_POST['radio1']) ) {
$filename = $_POST['radio1'] . "php";
header("Location: http://myaddress.com/".$filename);
}
Might be even better to set up an array with allowed values and check if radio1 is in that array.

Form submit to another page and then review or write to a file

I have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.

to call same form many times depending upon a value enterd by a user

I am now developing a travel agency website, in this site when the user reserves a trip he/she have to enter trip related details. This information is collected in a forma. The user also enters the number of people who are travelling.
My question is, how do I gather the same information for everyone who is travelling? Basically I need the form to be generated many times depending upon the number of people of same family ,, so I can capture all their data. How do I do this?
well there are my Code;plllz help me am so confused and tried lots of things to solve it;
thanks in advance
<form action = "insertpassenger.php" method = "POST">
<center>Enter all the information below</center>
<?php for ($i=0;$i<$pplno;$i++) : ?>
people<?php echo $i+1 ; ?>
<input type="text" name="cpr" size="9" value="<?php echo $cpr;?>" maxlength="9">CPR
<input type="text" name="pplno" size="30" maxlength="25">Number Of People
<input type="text" name="gcpr" size="9" maxlength="9">dad CPR
<input type="reset" value="clear" name="clear">
<input type="submit" value="join" name="join">
<?php endfor; ?>
</form>
Your code jumps in and out PHP rather a lot.
Just declare the item names as array entries, either with implicit or explicit numbering:
for ($i=0;$i<$pplno;$i++) : ?>
<input type="text" name="cpr[]" size="9" value="<?php echo $cpr[$i];?>" maxlength="9">CPR
<input type="text" name="pplno[]" size="30" maxlength="25">Number Of People
<input type="text" name="gcpr[]" size="9" maxlength="9">dad CPR
<input type="reset" value="clear[]" name="clear">
<input type="submit" value="join[]" name="join">
<?php endfor;
or...
for ($i=0;$i<$pplno;$i++) {
print "<input type=\"text\" name=\"cpr[$i]\" size=\"9\" value=\"$cpr[$i]\" maxlength="9">CPR";
....
}
I would personally use a session variable and count down how many times the form needs to be completed. Unfortunately that would cause to page to reload after each form entry, but this allows you to have as many amount of forms as your user requests, without a screen scrolling down a few pages to create all the forms on one page.
At the start of your code before you displaying anything to the browser :
<?php
session_start ();
?>
And where you receive your count for looping:
<?php
if (!isset($_SESSION['yourAppName']))
}
$_SESSION['yourAppName'] = $pplno;
} else {
$_SESSION['yourAppName']--;
}
if ($_SESSION['yourAppName'] > 0) {
?>
<form action=''>
<input type="text" name="cpr" size="9" value="<?php echo $cpr;?>" maxlength="9">CPR
<input type="text" name="pplno" size="30" maxlength="25">Number Of People
<input type="text" name="gcpr" size="9" maxlength="9">dad CPR
<input type="reset" value="clear" name="clear">
<input type="submit" value="join" name="join">
<input type="submit" value="Proceed">
</form>
<?php
} else {
// code when all forms are filled in
}
?>
remember to have your form return to the same page. This code is only to guide you, don't expect it to work without some editing. :)

Categories