I'm trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.
I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I'd ask: I can use $_POST['variableName'] to find the submitted variableName field-value; but...I don't know, or see, how to verify which form was used to submit the variables.
My code's below (though I'm not sure it's strictly relevant to the question):
<?php
$bmiSubmitted = $_POST['bmiSubmitted'];
if (isset($bmiSubmitted)) {
$height = $_POST['height'];
$weight = $_POST['weight'];
$bmi = floor($weight/($height*$height));
?>
<ul id="bmi">
<li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>
<li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>
<li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>
</ul>
<?php
}
else {
?>
<div id="formSelector">
<ul>
<li>Metric</li>
<li>Imperial</li>
</ul>
<form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (<abbr title="metres">m</abbr>):</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (Inches):</label>
<input type="text" name="height" id="height" /
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
}
?>
I verified that it worked (though without validation at the moment -I didn't want to crowd my question too much) with metric; I've added the form but not the processing for the imperial yet.
To identify the submitted form, you can use:
A hidden input field.
The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
You can use code as follows:
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
You can do it like this:
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">
Check the posted values
if (isset($_POST['myform'])) {
$values = $_POST['myform'];
// $login = $values['login'];
// ...
}
The form name is not submitted. You should just add a hidden field to each form and call it a day.
In the form submitting button (id method of form is post):
<input type="submit" value="save" name="commentData">
In the PHP file:
if (isset($_POST['commentData'])){
// Code
}
For some reason, the name of the submit button is not passed to the superglobal $_POST when submitted with Ajax/jQuery.
Use a unique value on the submit button for each form like so
File index.html
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="contact">Send Message</button>
</form>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="support">Send Message</button>
</form>
File email.php
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "contact":
break;
case "support":
break;
default:
break;
}
}
?>
As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.
To prevent an XSS attack, where you have written:
<?php echo "$weight"; ?>
You should write instead:
<?php echo htmlentities($weight); ?>
Which could even be better written as:
<?=htmlentities($weight); ?>
You can use GET in the form's action parameter, which I use whenever I make a login/register combined page.
For example: action="loginregister.php?whichform=loginform"
I had a similar problem which brought me to this question. I reviewed all the preceding answers, but ultimately I ending up figuring out my own solution:
<form name="ctc_form" id="ctc_form" action='' method='get'>
<input type="hidden" name="form_nm" id="form_nm">
<button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
</form>
It seamlessly and efficiently accomplishes the following:
Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
Works with both GET and POST methods.
Requires no additional, independent JavaScript.
You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.
Only the names of the form fields are submitted, but the name of the form itself is not. But you can set a hidden field with the name in it.
Related
I am building a WordPress plugin for my livechat. When someone downloads the plugin, I want them to fill out some information (name, e-mail, etc). After submitting that info, the form has to disappear/hide. For some reason I am not successful and imo I've tried everything. At the moment I'm trying to do it with an if-statement checking if the submit-button isset(). Unfortunately that didn't work.
Can someone please help me? The code for display the form and the page after submitting:
<?php
public function display_plugin_setup_page()
{
if (isset($_POST['submitForm'])) {
?>
<form action="options.php" method="post">
<?php
settings_fields('mister_chat_options');
do_settings_sections($this->plugin_name); ?>
<input name="submit" class="button button-primary" type="submit" value="<?php esc_attr_e('Save'); ?>" />
</form>
<?php
} else {
// create the form
?>
<form method="post" action="sendmail.php">
<input type="hidden" name="formSent">
<fieldset>
<input placeholder="Voornaam" type="text" id="vnaam" name="vnaam">
</fieldset>
<fieldset>
<input placeholder="Achternaam" type="text" id="anaam" name="anaam">
</fieldset>
<fieldset>
<input placeholder="Bedrijfsnaam" type="text" id="bnaam" name="bnaam">
</fieldset>
<fieldset>
<input placeholder="E-mailadres" type="email" id="email" name="email">
</fieldset>
<fieldset>
<input placeholder="Telefoonnummer" type="tel" id="telef" name="telef">
</fieldset>
<fieldset>
<input type="submit" name="submitForm" id="contact-submit" data-submit="...Verzenden">
</fieldset>
</form>
<?php
}
}
I placed the sendmail.php file inside the file above and that fixed my problem.
i have a problem keeping a post element in variables before calling one more time post in a second form .
briefly:
variable below contain elements from previous post
$img=isset($_POST['image'])?$_POST['image']:false;
$table=isset($_POST['tabl'])?$_POST['tabl']:false;
Then i have as next to the lines above the form below :
<form method="post" action="" style="padding-top:200px;padding-bottom:200px;padding-left:500px">
<div><span>Email</span></div> <input type="text" value="" name="user"/><br><br>
<div><span>Password</span></div> <input type="password" value="" name="pass"/><br><br>
<input type="submit" value="send" name="submit"/>
</form>
and right after pressing submit $img and $table are both empty .
how can i keep the values in $imgand $table even after calling post again ?
any clue ?
Thanks
You aren't sending the variables you want in the POST data in your second form.
You could use a hidden field to send it,
A bit like this.
<form method="post" action="" style="padding-top:200px;padding-bottom:200px;padding-left:500px">
<input type="hidden" name="img" value="<?php echo $img;?>" />
<input type="hidden" name="table" value="<?php echo $table;?>" />
<div><span>Email</span></div> <input type="text" value="" name="user"/><br><br>
<div><span>Password</span></div> <input type="password" value="" name="pass"/><br><br>
<input type="submit" value="send" name="submit"/>
</form>
I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)
I am trying to POST to a page that has two forms with duplicate name elements. The problem is that one form gets the password value and the other form gets the login value. (I can see this by printing out curl_exec($ch);) I will include my code for the target URL and the formdata. How do I fix this?
// my target url and form data
$target = "http://www.example.com/login";
$formdata = "id=$login&password=$password&Submit=Log In";
Forms:
<form id="login" name="login" method="post" action="login">
<label for="id">LOGIN ID</label> <input type="text" value="" name="id" maxlength="50" size="30"><br>
<label for="password">Password ID</label> <input type="password" name="password" maxlength="12" size="30">
<div align="center"><button class="siteSprite signInSm" value="Log In" name="Submit" type="submit"></button></div>
</form>
<form section="login" id="loginform" name="loginform" action="http://www.example.com/login" method="post">
<input type="text" size="20" value=" Log-in" onfocus="this.value=''" name="id"></td>
<input type="password" value="Password" maxlength="15" size="12" onfocus="this.value=''" name="password">
<input type="submit" class="siteSprite signInSm" value="Sign-In">
</form>
You'll have to do something to indicate which of the two forms got submitted. You can either submit a field with the same name but different values in each one, or use the submit button:
<form ...>
<input type="hidden" name="whichform" value="1" />
<input type="submit" name="Submit" value="form 1" />
</form>
<form ...>
<input type="hidden" name="whichform" value="2" />
<input type="submit" name="Submit" value="form 2" />
</form>
and then
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (($_POST['Submit'] == 'form 1') || ($_POST['whichform'] == '1')) {
.... handle form #1 ....
}
if (($_POST['Submit'] == 'form 2') || ($_POST['whichform'] == '2')) {
.... handle form #1 ....
}
using either method works the same, just pick the one that makes most sense/is easiest and go from there.
$formdata = "id=$login&password=$password&Submit=Sign-In"; might do the trick; note the fact that the second form has a submit button with a value, and the first form has a <button> which won't send a value (or, maybe, sends a different value via script or something)
I just noticed that the submit button doesn't have a name; try passing it with NO submit parameter, i.e.:
$formdata = "id=$login&password=$password
I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.