I have two forms contained in one PHP file. One of the forms is for "log in" and the other form is to "sign up" i have some CSS and JS that will switch out the forms visually, but regardless the two forms are still in one php file.
These forms are standard:
<form action="login.php" method="post" name="user-sign-up">
<div class="error">
<?php if(!empty($error)){echo $error;} ?>
</div>
<span>First name</span>
<input type="text" name="fname" class="fname">
<input type="submit" value="Sign Up" class="signupdawg" id="firstname-signup">
</form>
Then I got another form underneath that one.
Here's my php, it sort of works and only works for one of the forms:
if( isset($_POST['fname']) && !empty($_POST['fname'])){
$upName = $_POST['fname'];
if(empty($upName)){
$error = 'All fields are required';
I only shortened the code, so it wouldn't take up to much space. I had other inputs like a password, but I just removed it because the code is pretty much the same except I had an "or" in my php empty check and a few more lines in my html for it.
Anyway, I am wondering how I can validate two forms at once with PHP.
Thank you.
You can only submit one form at a time, so you need to use something on the form to determine which one was submitted. This is just a simple example using your code:
if(isset($_POST['Sign_Up'])) {
//do sign up stuff
}
elseif(isset($_POST['Login'])) {
//do login stuff
}
Notice Sign Up is converted to Sign_Up. It may be better to use two separate action in each form and have two different files.
Additionally, isset is redundant here:
if( isset($_POST['fname']) && !empty($_POST['fname'])){
The empty already checks if it is set, so just:
if(!empty($_POST['fname'])){
You need to use a unique value on the submit button for each form, an example is below
index.html
<form method="post" ...>
...
<button name="submit" type="submit" value="login">login</button>
</form>
<form method="post" ...>
...
<button name="submit" type="submit" value="sginup">sginup</button>
</form>
PHP file
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "login":
login();
break;
case "sginup":
singup();
break;
default:
break;
}
}
?>
Related
So what I want to do it kind of like a login form, but rather than it being individual users, it's more of a password locked page.
Here's sort of what I have for php
<?php
$user = $_POST['user'];
if($user == "placeholder")
{
include("randomfile.html");
}
else
{
if(isset($_POST))
{?>
<form id="login" method="POST">
User <input type="text" name="user" id="userID"></input><br/>
<input type="submit" name="submit" value="Go"></input>
</form>
<?}
}
?>
and it's basically doing what I want it to do, but if you were to go back (like use the go back button in the browser) it doesn't get rid of that submitted text (in this case, it would be "placeholder").
Any suggestions of any other way to do this, maybe easier or more basic because I just started with php, and is it possible so that if you enter "placeholder" and submit it, then go back, it doesn't have the User field already filled out with what you previously submitted?
<form id="login" method="POST" autocomplete="off">
That work for all the form, I think is the easiest. Ref: form:autocomplete
I have a form on on html outside of php...
<form method="post" action="">
<input type="text" name="user"/></br>
<input type="submit" value="submit" name="login"/>
</form>
then call submit button from php and do this
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply"/>
</form>
this;
if(isset($_POST["apply"]))
{ print "it works";}
}
Alright, so the problem is that, "it works" won't print from the second form thats inside the php. it just takes me back to where i came from. Perhaps it's a dumb question, please help though! thanks
The problem is that by the time you're checking if(isset($_POST["apply"])) the login condition becomes invalid because everything is inside the if(isset($_POST["login"])).
Try taking the if(isset($_POST["apply"])) outside the login IF.
Your "apply" code exists only INSIDE the login test code. When you submit that second form, there will be NO login form field, because you didn't include an input/textarea of that name in the second form. So the second form submits, there's no login, and the entire inner code never gets executed. You probably want:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="" name="apply">
<input type="hidden" name="login" value="foo" /> <!-- add this line -->
etc...
I'm not sure to understand what you wanna do with this code but you obviously missed some details :
_You did not set the "action" field in your form tag, so I don't understant how you would like the PHP file to get called ?
_Your code if(isset($_POST['login'])) has no sense, you are testing the existence of a value sent by a validation button, you'd rather whrite isset($_POST['user'])
Hoping to have helped you
Your variables are declared in 2 forms, so there will be 2 calls (completely independant) to your php.
So you could have a second submit button inside your second form:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply" value="Second"/>
</form>
this;
}
if(isset($_POST["apply"]))
{ print "it works";}
So here's my full code
<!DOCTYPE html>
<html>
<body>
<h1>Encrypt</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter word to encrypt<input type="text" name="in">
<input type="submit">
<hr>
</form>
<h1>Decrypt</h1>
<form>
Enter word to decrypt<input type="text" name="out">
<input type="submit">
<hr>
</form>
</body>
</html>
<?php
$encrypt = $_POST['in'];
?>
And here's the error I get
Notice: Undefined index: in in /Users/idrisk/Colourity/si/index.php on line 20
Line 20 is $encrypt = $_POST['in']; and I don't see what I'm doing wrong with it. Any ideas?
As a general practice for forms in php, always check if the submit button has been clicked.
First name your submit button:
<input type="submit" name="submit">
then further in your php:
if (isset($_POST['submit'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
EDIT #1: Added to that, you seem to have 2 forms and 2 submit buttons. I suggest you keep only one form, and one submit button (remove the 2nd form element and submit button).
If you really need 2 forms, name your submit buttons differently and then you can call them separately.
<input type="submit" name="submit-in">
<!-- ... -->
<input type="submit" name="submit-out">
<?php // ...
if (isset($_POST['submit-in'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
if (isset($_POST['submit-out'])) {
// do your stuff, eg...
$dencrypt = $_POST['out'];
}
EDIT #2: If you want to echo stuff posted in your form, make sure you do the form submission checking and variable setting before the form and then echo the variable after the form (or wherever you want).
you need to first check if the form has been sent, if it hasn't then $_POST['in'] does not yet exist thus throwing the error
May be nothing but you called a php script after closing the form /form, the body /body and then then the HTML /html
replace this code $encrypt = $_POST['in']; by this $encrypt = #$_POST['in'];
this is an error on client server when you upload this file on remote server you will not saw this. use # sign on the client server when you saw this error in future.
I have two input fields and whenever I open my page, it displays errors since at the start user has not entered any input to any of the field (& the errors are displayed because the user input is used in sql queries to retrieve data).
I just want to display those two forms at start of the page not the errors.
Both inputs are required to execute the Compare button. If user has not entered either one of the inputs it should not send request to php for scripting.
I mean the Compare button should send request only if both inputs are filled otherwise it should give a message to user to Type the required fields.
How to do this?
$trimUser= trim ($_POST['name']);
if(empty($name) || empty($name2))
{
echo "Enter a name ";
}
else if (isset($_POST['name']))
{
$name=$_POST['name'];
}
else if (isset($_POST['name2']))
{
$name2=$_POST['name2'];
}
& here is my form:
<form action="index.php" method="POST">
<input class="span3 search-query" placeholder="Type User A" type="text" name="name" id="field"/
<input class="span3 search-query" placeholder="Type User B" name="name2" type="text"
id="field2"/>
<button class="btn btn-primary" data-loading-text="Loading..." >Compare</button>
You have to use java script or jQuery for validate both fields are not empty. For Example..
<form action="index.php" method="POST" onsubmit="return validate()">
<input class="span3 search-query" placeholder="Type User A" type="text" name="name" id="field"/>
<input class="span3 search-query" placeholder="Type User B" name="name2" type="text"
id="field2"/>
<button class="btn btn-primary" data-loading-text="Loading..." >Compare</button>
</form>
<script type="text/javascript">
function validate(){
var field1 = document.getElementById('field').value;
var field2 = document.getElementById('field2').value;
if(field1 != '' && field2 != '' ){
return true;
} else{
alert('Type the required fields');
return false;
}
}
</script>
Here if Both fields are not empty then it will be allow to submit form. And In PHP script Add
if(isset($_POST) && !empty($_POST)){
//code comes here
}
I hope it will be helpful for you.
thanks
You can add a check to verify if the request is a post request :
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Place your error checking code here
}
Ehs4n is right, but I would be more specific and do something like :
if(!empty($_POST['compare'])) {
#validation
}
Your button code would have to be changed to :
<button name="compare" value="1" class="btn btn-primary" data-loading-text="Loading..." >Compare</button>
There are two reasons I would do this:
Using !empty() makes sure you don't get an error when $_POST['compare'] is empty
Checking $_POST['compare'] instead of just $_POST makes sure errors are only shown if someone clicks the button.
This last point is key because if you have multiple forms on the page or you happen to set a $_POST variable elsewhere you would still be showing errors.
Use the if condition with isset($_POST) before loading the post.i.e.,
if (isset($_POST)) {
if(empty($name) || empty($name2))
{
echo "Enter a name ";
}
else if (isset($_POST['name']))
{
$name=$_POST['name'];
}
else if (isset($_POST['name2']))
{
$name2=$_POST['name2'];
}
I simply got rid all of all the errors by adding this error_reporting(E_ERROR | E_PARSE); at the start of my code.
However if anyone want to check display validation error messages , one can do easily by what others have mentioned . i.e By using if($_Post).
Anyway ,Thank you everyone for the help.
Add if clause like
if($_POST) {
...your validation code
}
Think of redirecting people AFTER the error to the same page they were:
echo '<script>location.href=\'example.php\'</script>';
I have a project that requires a search form that searches two separate eBay stores depending on what the user is looking for.
I want to have one input field with two submit buttons. I'm having problems getting the form to specify which site to call from either of the buttons. I've had a good look around and tried a few things but thought someone might be able to straighten out my syntax etc.
I have the form.
<form target="_blank" action="rp.php" method="get">
<input type="text">
<button name="replacement" type="submit">Search Replacement Parts</button>
<button name="performance" type="submit">Search Performance Parts</button>
</form>
..and I have the PHP.
<?php
if( isset($_GET['replacement']) ) {
header("Location: http://www.example.com");
exit;
} else if {
( isset($_GET['performance']) ) {
header("Location: http://www.example.com");
exit;
}
?>
I think I'm on the right track, just need a bit-o-help. I don't know whether or not to use POST or GET, GET seemed to make more sense to me for this.
You can use the following code for this:
<form target="_blank" name="myform" method="POST">
<input type="text" name="search">
<button onclick="submit1('http://example1.com/')" >Search Replacement Parts</button>
<button onclick="submit1('http://example2.com/')" >Search Performance Parts</button>
</form>
<script>
function submit1(url)
{
document.myform.action=url;
document.myform.submit();
}
</script>
Your PHP is a bit off for the elseif, otherwise it should "work"
<?php
if ( isset($_GET['replacement']) ) {
header("Location: http://www.example.com");
exit;
} elseif ( isset($_GET['performance']) ) {
header("Location: http://www.example.com");
exit;
}
?>
You can use either a GET or a POST to do this, however a GET will result in the query values being visible in the querystring and are limited in size in some older browsers. POST is probably prefered, in which case you would replace $_GET with $_POST in the code above, and change method="get" to method="post" in your form.
I would use same name for both buttons, with different value for each one and then in php code just check which value is submitted.