I want to place both register and login form on the same page.
They both starts with:
if (!empty($_POST)) ...
so, I need something like:
if (!empty($_POST_01))... // regForm
and
if (!empty($_POST_02))... //loginForm
Also how to prevent executing first form if the second is busy, and vice versa (user clicks on both)
My idea is to create a simple variable on starting process, for example $x = 1 and at the end of process $x = 0, so:
if ((!empty($_POST_01)) And $x = 0)...
Probably, there is a better way.
You could make two forms with 2 different actions
<form action="login.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
<br />
<form action="register.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Register">
</form>
Or do this
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="login">
<input type="submit" value="Login">
</form>
<br />
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="register">
<input type="submit" value="Register">
</form>
Then you PHP file would work as a switch($_POST['action']) ... furthermore, they can't click on both links at the same time or make a simultaneous request, each submit is a separate request.
Your PHP would then go on with the switch logic or have different php files doing a login procedure then a registration procedure
Well you can have each form go to to a different page. (which is preferable)
Or have a different value for the a certain input and base posts on that:
switch($_POST['submit']) {
case 'login':
//...
break;
case 'register':
//...
break;
}
Give the submit buttons for both forms different names and use PHP to check which button has submitted data.
Form one button - btn1
Form two button -btn2
PHP Code:
if($_POST['btn1']){
//Login
}elseif($_POST['btn2']){
//Register
}
You can use this easiest method.
<form action="validator.php" method="post" id="form1">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="submit" form="form1">
</form>
<br />
<form action="validator.php" method="post" id="form2">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="submit" form="form2">
</form>
Here are two form with two submit button:
<form method="post" action="page.php">
<input type="submit" name="btnPostMe1" value="Confirm"/>
</form>
<form method="post" action="page.php">
<input type="submit" name="btnPostMe2" value="Confirm"/>
</form>
And here is your PHP code:
if (isset($_POST['btnPostMe1'])) { //your code 1 }
if (isset($_POST['btnPostMe2'])) { //your code 2 }
Hope this will help you. Assumed that login form has: username and password inputs.
if(isset($_POST['username']) && trim($_POST['username']) != "" && isset($_POST['password']) && trim($_POST['password']) != ""){
//login
} else {
//register
}
Related
for the life of me cant remember how i should be doing this / if its possible.
I am creating a form that takes the user to another page but want to pass the value of the text box in the url.
so e.g:
<form action="newadvert.php?vrm=" class="newadvertform" method="post">
<input type="text" name="carvrm" class="carvrm"/>
<input type="submit" class="newadvertsubmit" value="Create Advert" />
</form>
How can i send the user to newadvert.php?vrm= THE ENTERED TEXT HERE
I need this to then get value on the next page.
Use GET method
<form action="newadvert.php" class="newadvertform" method="GET">
And rename carvrm to vrm
<input type="text" name="vrm" class="carvrm"/>
Use the GET method and rename carvrm to vrm.
<form action="newadvert.php" class="newadvertform" method="GET">
<input type="text" name="vrm" class="carvrm"/>
<input type="submit" class="newadvertsubmit" value="Create Advert" />
</form>
When submitting the form, it'll go as http:///newadvert.php?vrm=
<form action="newadvert.php" class="newadvertform" method="get">
<input type="text" name="vrm" class="carvrm"/>
<input type="submit" class="newadvertsubmit" value="Create Advert" />
</form>
change your form method from post to get
Like this:
<form action="newadvert.php" class="newadvertform" method="get">
<input type="text" name="carvrm" class="carvrm"/>
<input type="submit" class="newadvertsubmit" value="Create Advert" />
</form>
you can get input value by :
$_REQUEST['carvrm']
or change input name to vrm :
<form action="newadvert.php" class="newadvertform" method="get">
<input type="text" name="vrm" class="carvrm"/>
<input type="submit" class="newadvertsubmit" value="Create Advert" />
</form>
now you can get input value by :
$_REQUEST['vrm']
doc: http://www.w3schools.com/PHP/php_forms.asp
Problem: How to make an HTML Form call different php pages from the action based on what button is pushed?
The code below is the solution I have now, but I figure there must be a better way to do this then creating multiple forms on the page?
<html>
<body>
<form name="entry_form" action="entry_update_script.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="entry_id" value="">
<input type="hidden" name="entry_item_id" value="">
Truck/Railcar/Barge#:<input type="text" name="pro_number" value=""><br>
BOL #:<input type="text" name="bol" value=""><br>
<input type="submit" name="entry_submit" value="Add New Entry!">
</form>
<form name="entry_form_add" action="entry_view.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="entry_id" value="">
<input type="submit" name="submit" value="Add New Item!">
</form>
</body>
</html>
<html>
<body>
<script type="text/javascript">
function submitAction(act) {
document.sample.action = act;
document.sample.submit();
}
</script>
<form name ="sample" action="default.php">
<input type="button" value = "blah1" onClick="submitAction('phpPage1.php')">
<input type="button" value = "blah2" onClick="submitAction('phpPage2.php')">
</form>
</body>
</html>
you might choose the page to go from a dispatcher, it's an extensible and robust solution:
your form
<form action="dispatcher.php" method="POST">
<input type="radio" name="myOption" value="register" />
<input type="radio" name="myOption" value="login" />
</form>
dispatcher.php
$actions = array ('register', 'login');
// validate possible actions
if (in_array($_POST['myOption']), $actions)) {
include ($_POST['myOption'] . '.php');
}
I need to have a form that when filled out will create a variable and then goto a url with that variable in the url.
Something like this (but that works) :)
<form action="?????" method="?????">
Number: <input type="text" name="url1" value=""><br>
<input type="submit" name="submit" value="Goto URL">
</form>
When the submit is pressed I need it to goto http://somewhere.com?url=VALUEHERE
Any ideas?
Use method="GET" to place the variables in the url:
<form action="http://somewhere.com/" method="GET">
Number: <input type="text" name="url" value="" /><br />
<input type="submit" name="submit" value="Goto URL" />
</form>
Posting this form will go to http://somewhere.com/?url=USER_INPUT_URL
Different from 1st. No form needed, add an attribute named 'id' for the textfield,then defined a javascript function to retrieve the value of textfield,then do the jump, Hope usefully.
<form action="?????" method="?????">
Number: <input id="url1" type="text" name="url1" value=""><br>
<input type="button" name="submit" value="gotoURL()">
</form>
<script>
function gotoURL(){
window.location='http://somewhere.com?url='+document.getElementById('url1').value;
}
</script>
<form>
Number: <input type="text" name="url1" id="url1" value=""><br>
<input type="submit" name="submit" value="Goto URL" onclick="redirect()">
</form>
<script>
function redirect()
{
window.location='http://somewhere.com?url=' + document.getElementByID('url1').value;
}
</script>
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 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.