Multiple form and php link - php

I have a problem with my form. So in my logandreg.html I have 2 forms. One for login which is
<form method="post" action="valid.php">
<p>
Username:
<input type="text" name="login_username">
</p>
<p>
Password:
<input type="password" name="login_pass">
</p>
<div class="clear"></div>
<div class="form-row login">
<p>
<input type="submit" value="Login" id="login" name="login" class="button alt">
</p>
and another form for register
<form method="post" action="register.php">
<p>
Username:
<input type="text" name="username">
</p>
<p>
Password:
<input type="password" name="pass">
</p>
<p>
E-mail:
<input type="text" name="email">
</p>
<div class="clear"></div>
<div class="form-row reg">
<p>
<input type="submit" data-value="Register" value="Register" id="reg" name="reg" class="button alt">
</p>
But why when I click on the submit button on the register part it always bring me to valid.php which is from the login form? Both of these form are in the same html. In the valid.php I have this
<?php
session_start();
if(isset($_POST['login']))
{...}
?>
and in the register.php I have this
<?php
if(isset($_POST['reg']))
{...}
?>
Why does it always bring me to the valid.php when I click on the register button? Where is my mistake? Thanks

Try to submit both of form using button, and trigger click event then relevent form submit event.
<form name="login" id="login" method="post" action="do_login.php">
<input type="text" name="inp_1" id="inp_1"/>
<button type="button" id="login_submit"></button>
</form>
<form name="registration" id="registration" method="post" action="do_registration.php">
<input type="text" name="inp_2" id="inp_2"/>
<button type="button" id="registration_submit"></button>
</form>
<script type="text/javascript">
$("#login_submit").click(function(){
$("#login").submit();
});
$("#registration_submit").click(function(){
$("#registration").submit();
});
</script>

Related

tag <a onclick = file.php> and refresh page

I have a login form; this is a part of my code:
<form method="post" action="login.php" >
<label>Username</label>
<input type="text" name="username"/>
<br>
<label>Password</label>
<input type="password" name="password"/>
<br>
<div class="log"><a class="btn btn_red">Login</a></div>
</form>
What I expect is that this form redirect me to the "login.php" file, but it doesn't work. Where am I going wrong?
Thank u all.
You have an anchor (with no href attribute) and no button.
You need a submit button
<div class="log"><button class="btn btn_red">Login</button></div>
Try this
<form method="post" action="login.php" id="form1">
<label>Username</label>
<input type="text" name="username"/>
<br>
<label>Password</label>
<input type="password" name="password"/>
<br>
<div class="log"><button type="submit" class="btn btn_red" form="form1" value="Submit">Login</button></div>
</form>

Error "End tag fieldset seen, but there were open elements.”

I was running the W3C validator on the internal page of site and I received this error regarding fieldset tag:
End tag fieldset seen, but there were open elements.
</fieldset>
Please help me with the corect code.
My code:
<div class="login_header">
<form name="form_login" id="form_login" method="post" action="<?php echo base_url(); ?>login">
<fieldset>
<input type="text" value="" id="user_name" name="user_name" placeholder="Email/Username"/>
</fieldset>
<fieldset>
<input type="password" value="" id="password" name="password" placeholder="Password"/>
</fieldset>
<fieldset class="loginBtnHld">
<input type="submit" name="btn_submit" id="btn_login_submit" value="Login"/>
<input type="button" value="Join Free" onClick="window.location.href = '<?php echo base_url(); ?>registration';">
<div class="login_links">Forgot password?
</fieldset>
</form>
</div>
</div>
Perhaps it's a missing </div> after </a> and before </fieldset>.
<div class="login_links">Forgot password?
</fieldset>

not able to add data from web page to database

this is my form .html file getting connected to adddatabase.php page .having firstname and secondname
<form action="addtodatabase.php" method="post">
<div class="container">
<form class="form-inline">
<fieldset>
<legend>Security Department User Registration</legend>
<div class="form-group">
<label for="Firstname">First Name</label>
<input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/>
</div>
<div class="form-group">
<label for="Secondname">Second Name</label>
<input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/>
</div>
</form>
<button type="submit" class="btn btn-default">Submit</button>
</form>
addtodatabase.php page as follows .
if (isset($_POST)) {
$firstname = isset($_POST['firstname']) ? $_POST['firstname'] : '';
$secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';
echo 'Your first name is ' .$firstname. '<br>';
echo 'Your second name is ' .$secondname. '<br>';
}
mysqli_query($connect,"INSERT INTO form_details(firstname,secondname)
VALUES('$firstname','$secondname')");
You have form inside a form which is wrong. Do it like this:
<form method="POST" action="addtodatabase.php" >
<!-- Your inputs -->
</form>
Also, put your submit button inside the form:
<form method="POST" action="addtodatabase.php" >
<!-- Your inputs -->
<input type="submit" value="Submit" />
</form>
Or if the submit button is outside, set the form attribute of the button to the id of the form:
<form id="myForm" method="POST" action="addtodatabase.php" >
<!-- Your inputs -->
</form>
<input type="submit" value="Submit" form="myForm" />

How to hide and show multi registration

I try to make a multi registration form in 2 steps.
And let work the buttons.
How do I hide step two until proceed step 1, than hide step 1.
<div class="user-register-field-1">
<form name="register" action="<?php echo osc_base_url(true); ?>" method="post">
<fieldset>
<input type="hidden" name="page" value="register" />
<input type="hidden" name="action" value="register_post" />
<button type="submit" class="small radius">
<?php _e("Continue Step 2", 'ctg_housing');?>
</button>
</fieldset>
</form>
</div>
<div class="user-register-field-2">
<form name="user-register-2" action="<?php echo osc_base_url(true); ?>" method="post">
<fieldset>
<input type="hidden" name="page" value="register" />
<input type="hidden" name="action" value="register_post" />
<button type="submit" class="small radius">
<?php _e("Create Account", 'ctg_housing');?>
</button>
</fieldset>
</form>
</div>
Thanks
Try this:
HTML:
<div class="user-register-field-1">
<form name="register" action="<?php echo osc_base_url(true); ?>" method="post">
<fieldset>
<input type="text" name="page" >
<input type="text" name="action" >
<button type="submit" class="small radius">
<?php _e("Continue Step 2", 'ctg_housing');?>
</button>
</fieldset>
</form>
</div>
<div class="user-register-field-2">
<form name="user-register-2" action="<?php echo osc_base_url(true); ?>" method="post">
<fieldset>
<input type="text" name="page">
<input type="text" name="action">
<button type="submit" class="small radius">
<?php _e("Create Account", 'ctg_housing');?>
</button>
</fieldset>
</form>
</div>
CSS:
.user-register-field-2{
display:none;
}
JS:
$(document).ready(function(){
$("div.user-register-field-1 form").on("submit", function(){
$("div.user-register-field-1").hide();
$("div.user-register-field-2").show();
});
});
Check also my Fiddle: https://jsfiddle.net/cz5kyaqL/
I hope I understood you right and could help

$_POST not being read

i have a form which inserts book information into the database. however it is not reading the $_POST attribute.
book.php:
<form action="books_manage.php" id="addbook_form" method="post">
<div id="ab_wrapper">
<div id="ab_leftcolumn">
<div id="bookinfo">
<fieldset>
<legend>Book Details</legend>
<div class="field">
<label>Book ID</label>
<input type="text" name="bid" id="bid"/>
</div>
<div class="field">
<label>Name</label>
<input type="text" name="bname" id="bname"/>
</div>
<div class="field">
<label>Author</label>
<input type="text" name="bauthor" id="bauthor"/>
</div>
<div class="field">
<label>Info</label>
<textarea name="binfo" id="binfo" cols="5" rows="5" ></textarea>
</div>
<div class="field">
<label>Date Added</label>
<input type="text" value="<?php echo date('D d M Y')?>" name="bdateadd" id="bdateadd"/>
</div>
<div class="field">
<label>Date Updated</label>
<input type="text" value="<?php echo date("D d M Y")?>" name="bdateupd" id="bdateupd"/>
</div>
<div>
<input type="hidden" name="action" value="save">
<input type="submit" value="Save">
<input type="button" id="addcontent" value="Add Content">
<input type="reset" value="Reset">
</div>
</fieldset>
</div>
</div>
<div id="ab_rightcolumn">
<div id="bookcontents">
<fieldset>
<legend>Book Content</legend>
<div class="field">
<label>Chapter</label>
<input type="text" id="bchapter" name="bchapter"/>
</div>
<div class="field">
<label>Sub-Chapter</label>
<input type="text" id="bsubchapter" name="bsubchapter"/>
</div>
<div class="field">
<label>Content</label>
<textarea id="bcontent" name="bcontent" rows="6" cols="8"></textarea>
</div>
<br />
<div>
<input type="hidden" name="action" value="addnext">
<input type="submit" value="Save and Add Next Chapter">
<input type="submit" name="action" value="Done">
</div>
</fieldset>
</div>
</div>
</div>
</form>
books_manage.php:
<?php
if (isset($_POST['action']) && $_POST['action'] == 'save')
{
echo "You clicked the save button";
}
else {
echo "Hello. The date is " . date("D d M Y") ;
}
?>
the output:
Hello. The date is Thu 08 Jul 2010
it seems it isn't reading the value of the hidden button. it should display "You clicked the save button". Am I missing something?
First of all, multiple <input>s in the same <form> with the same name attribute isn't going to get you the behavior you're looking for.
Instead, you need to provide a name to the submit buttons, and then you can check which button was pressed:
<input type="submit" name="save" value="Add Content">
<input type="submit" name="done" value="No more content">
<?php
if(isset($_POST['save'])) {
echo "saved";
} else if(isset($_POST['done'])) {
echo "done";
}
?>
See comment below by Lèse majesté to learn how the HTML working group effed this one up.
You have two inputs with the name "action" in the same form. Make sure your form field names are unique.
Don't forget you can organise your names using this syntax -
<input name="form1['name']" value="".....
<input name="form2['name']" ..... etc
Then access these variables like this:
$_POST['form1']['name']...
Very useful!
Its becuase you have defined action 3 times
<input type="hidden" name="action" value="save">
<input type="hidden" name="action" value="addnext">
<input type="submit" name="action" value="Done">
Do the followin on your books_manage.php
echo "<pre>";
print_r($_POST);
echo "</pre>";
You will see where you are going wrong.
<input type="hidden" name="action" value="addnext">
<input type="submit" value="Save and Add Next Chapter">
<input type="submit" name="action" value="Done">
you have two inputs with name "action". the action you get is probably "Done", not "save"
You have multiple inputs named action you will get
<input type="hidden" name="action" value="save">
<input type="hidden" name="action" value="addnext">
<input type="submit" name="action" value="Done">
You need to remove the hidden variables and change the name of your first 'submit' to 'action'
<input type="submit" name="action" value="Save">
<input type="submit" name="action" value="Done">
You need to name your submit button "action" and use the value of that button to determine the action. Your code basically has two action form values and the last one is what takes precedence.
<form action="books_manage.php" id="addbook_form" method="post">
<div id="ab_wrapper">
<div id="ab_leftcolumn">
<div id="bookinfo">
<fieldset>
<legend>Book Details</legend>
<div class="field">
<label>Book ID</label>
<input type="text" name="bid" id="bid"/>
</div>
<div class="field">
<label>Name</label>
<input type="text" name="bname" id="bname"/>
</div>
<div class="field">
<label>Author</label>
<input type="text" name="bauthor" id="bauthor"/>
</div>
<div class="field">
<label>Info</label>
<textarea name="binfo" id="binfo" cols="5" rows="5" ></textarea>
</div>
<div class="field">
<label>Date Added</label>
<input type="text" value="<?php echo date('D d M Y')?>" name="bdateadd" id="bdateadd"/>
</div>
<div class="field">
<label>Date Updated</label>
<input type="text" value="<?php echo date("D d M Y")?>" name="bdateupd" id="bdateupd"/>
</div>
<div>
<input type="submit" name="action" value="Save">
<input type="button" id="addcontent" value="Add Content">
<input type="reset" value="Reset">
</div>
</fieldset>
</div>
</div>
<div id="ab_rightcolumn">
<div id="bookcontents">
<fieldset>
<legend>Book Content</legend>
<div class="field">
<label>Chapter</label>
<input type="text" id="bchapter" name="bchapter"/>
</div>
<div class="field">
<label>Sub-Chapter</label>
<input type="text" id="bsubchapter" name="bsubchapter"/>
</div>
<div class="field">
<label>Content</label>
<textarea id="bcontent" name="bcontent" rows="6" cols="8"></textarea>
</div>
<br />
<div>
<input type="submit" name="action" value="Save and Add Next Chapter">
<input type="submit" name="action" value="Done">
</div>
</fieldset>
</div>
</div>
</div>
<?php
if (isset($_POST['action']) && $_POST['action'] == 'Save')
{
echo "You clicked the save button";
}
else if (isset($_POST['action']) && $_POST['action'] == 'Save and Add Next Chapter')
{
echo 'You clicked the "Save and Add Next Chapter" button';
}
else if (isset($_POST['action']) && $_POST['action'] == 'Done')
{
echo 'You clicked the done button';
}
else
{
echo "Hello. The date is " . date("D d M Y") ;
}
?>

Categories