Reputation: 13
I am trying to create a single dynamic webpage with two separate forms:
This would mean one submit button for each form and clicking on one submit button should collect data from one form only. I tried the approach shown below but the data was not submitted to mySql database. I have tried running the forms individually and they do work with mysql database so I guess there should be something else wrong. The code below is stripped of details but I reckon the gist of it is inside. Thanks in advance!
<?php
if(isset($_POST['submit1']))
{
doSomething();
}
?>
<form id="form1" name="form1" action="" method="post">
...
<input type="submit" name="submit1" value="Sign in"/>
</form>
<?php
if(isset($_POST['submit2']))
{
doSomethngElse();
}
?>
<form id="form2" name="form2" action="" method="post">
...
<input type="submit" name="submit2" value="Sign up"/>
</form>
Upvotes: 1
Views: 984
Reputation: 7752
try first checking for post request
<?php
if($_POST)
{
echo "post occurs"; // only for testing to check whether post is occur or not
if(isset($_POST['submit1']))
{
doSomething();
}
else if(isset($_POST['submit2']))
{
doSomethngElse();
}
else
{
echo "no form is posted";
}
}
else
{
echo "Post not happened";
}
?>
<form id="form1" name="form1" action="" method="post">
...
<input type="submit" name="submit1" value="Sign in"/>
</form>
<form id="form2" name="form2" action="" method="post">
...
<input type="submit" name="submit2" value="Sign up"/>
</form>
Upvotes: 1
Reputation: 36
So I do like this:
Use 2 button with 1 name :
//========== first form ==========
<form name="form1" action....>
fields
<input type="submit" name="_Submit" value="Login">
</form>
// ========== second form ==========
<form name="form2" action....>
fields
<input type="submit" name="_Submit" value="Sign up">
</form>
// ========== in php file do this ==========
<?php
$submit = $_POST["_Submit"];
if($submit=="Login"){
// get login form data
}else if($submit=="Sign up"){
// get sign up form data
}else{
// submit is empty or undefined value
}
?>
Upvotes: 1
Reputation: 570
Like I said, you can check if the values are empty so you could just do
if(isset($_POST['submit2']) == ""){
//The variable is empty
}
else
{
//The variable is not empty
}
And if i correct you could just do
if (!(empty($_POST['submit2'])){
//This variable is not empty
}
Upvotes: 0
Reputation: 33512
Not that this answers your question directly, I think the rest of your code would be needed for that... but...
Why not just check a value in a single form element like this:
<?php
if(isset($_POST['submit1']))
{
if(!empty($_POST['submit1']) && $_POST['submit1']=='SignIn')
{
// Do Some Code... like:
?>
<form id="form1" name="form1" action="" method="post">
...
<input type="submit" name="submit1" value="SignIn"/>
</form>
<?php
}
if(!empty($_POST['submit1']) && $_POST['submit1']=='SignUp')
{
// Do Some Other Code...
?>
<form id="form2" name="form2" action="" method="post">
...
<input type="submit" name="submit1" value="SignUp"/>
</form>
<?php
}
}
?>
Upvotes: 0