Form with two buttons that needs two different functions - php

I have a login form here with two buttons namely login and register. Both of them are under a form so the form has an action and both of them are doing the same function. I need them to be linked to my different php pages. Here is my code:
<table>
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table>
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td">Username</td>
<td>:</td>
<td><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit1" value="Login"><input type="submit" name="Submit2" value="Sign Up""></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
If I click on my login button, I want it to be linked to "checklogin.php" which is same as the link on the field. And if I click on sign up, it will be linked to "signup.php".

Submit to one place. Worry about what to do with the data on the server.
<?php
if (isset($_POST['Submit1'])) {
include('login.php');
} else if (isset($_POST['Submit2'])) {
include('signup.php');
} else {
# default behaviour
}

My advise is don't use any of the buttons as a submit button.
Use the onclick and link to 2 separate functions - login() and register().
Inside the login() and register() function you can use jquery to submit the form to the a different url from each function.
In your Javscript you need to define the functions
<script>
function login() {
//make form post here
}
function register() {
//make form post here
}
</script>
Read this answer to get more details on how to submit a form using Ajax.
Hope this helps!

Without changing server side code this can be easily achieved by using javascript to change form action attribute before submitting. Try this pattern:
<input type="submit" onclick="this.form.action = 'another_url';" />

this functionality can be achieved using JavaScript this is one of the option.If you use javascript you need not use form tag.
<input type="button" name="Submit1" value="Login" onclick="login()">
<input type="button" name="Submit2" value="Sign Up" onclick="signup()">
JS Functions:
function login()
{
window.location = 'http://www.yourdomain.com'
}
function signup()
{
window.location = 'http://www.yourdomain.com'
}
Just remind to give type="button" instead of type="submit"
You can carry out validations for the form if you use JS.Hope this helps.

You can create a function in JavaScript linktoURL. Call that function on button onClick event and pass the URL as the parameter to that function where you want to redirect
function linktoURL(url)
{
document.form1.action = url;
document.form1.sumbit();
}
<table>
<tr>
<form name="form1" id="form1" method="post" action="checklogin.php">
<td>
<table>
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td">Username</td>
<td>:</td>
<td><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<input type="button" name="Submit1" value="Login" onclick="linktoURL('checklogin.php');">
<input type="button" name="Submit2" value="Sign Up" onclick="linktoURL('signup.php');"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Related

Is it possible to use post and get function in one php form?

<html>
<body>
<div class="content">
<fieldset>
<div>
<h1>Student Details</h1>
</div>
<form method="">
<table>
<tr>
<td><label>Student ID:</label></td>
<td><input type="text" name="StuID"></td>
<td><input type="submit" name="find" value="Find"></td>
</tr>
<tr><td></td></tr>
<tr>
<td><label>Student IC:</label></td>
<td><input type="text" name="StuIC"></td>
</tr>
<tr>
<td><label>School ID:</label></td>
<td><input type="text" name="SchID"></td>
</tr>
<tr>
<td><label>Student Name:</label></td>
<td><input type="text" name="StuNAME"></td>
</tr>
<tr>
<tr>
<td><label>Gender:</label></td>
<td>
<select name="Gender">
<option>--Gender--</option>
<option>Male</option>
<option>Female</option>
</select>
</td>
</tr>
<tr>
<td><label>Nationality:</label></td>
<td><input type="text" name="Nationality"></td>
</tr>
<tr>
<td><label>Race:</label></td>
<td>
<select name="Race">
<option>--Race--</option>
<option>Malay</option>
<option>Indian</option>
<option>Chinese</option>
<option>Other</option>
</select>
</td>
</tr>
<tr>
<td><label>Religion:</label></td>
<td>
<select name="Religion">
<option>--Religion--</option>
<option>Muslim</option>
<option>Buddist</option>
<option>Hindu</option>
<option>Christian</option>
<option>Other</option>
</select>
</td>
</tr>
<tr>
<td><label>Telephone Number:</label></td>
<td><input type="text" name="TelNo"></td>
</tr>
</tr>
<tr>
<td><input type="submit" name="add" value="Add"></td>
<td><input type="submit" name="update" value="Update"></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
</fieldset>
</div>
</body>
</html>
This is my form, the user can find the data by insert the ID and click "Find" and the data will display in the form. With the same form, user can update the data with just change data in the form and click "Update" and the data will update in database.
Is it possible for me to build a form that can do Insert, Update and Delete using just one php form.
You can create one form, and with JS/jQuery (or any other lib) submit that form via Ajax depending on button clicked:
<form id="my-form">
...
<tr>
<td><label>Student ID:</label></td>
<td><input type="text" name="StuID" id="stu-id"></td>
<td><input type="submit" name="find" value="Find" onClick="sendPost()"></td>
</tr>
<tr>
<td><input type="button" name="add" value="Add" onClick="sendPost()"></td>
<td><input type="button" name="update" value="Update" onClick="sendPut()"></td>
<td><input type="button" name="delete" value="Delete" onClick="sendDelete()"></td>
</tr>
</form>
function sendFind() {
$.ajax({
url: 'example.com/my-action?StuID=' + $('#stu-id').val(),
method: 'GET',
success: function () {}
...
});
}
function sendPut() {
$.ajax({
url: 'example.com/my-action',
method: 'PUT',
dataType: 'json',
data: $("#my-form").serialize(),
success: function () {}
...
});
}
function sendDelete() {
$.ajax({
url: 'example.com/my-action',
method: 'DELETE',
dataType: 'json',
data: $("#my-form").serialize(),
success: function () {}
...
});
}
As you can see there even can be same URL and act differently depending on request type.
Its not possible though you can use $_REQUEST in php to get any posted data whether with POST or GET.
For your case you can name your input type submit with different names then check in your php
<?php
if(isset($_POST['insert'])){ //insert functionality }
if(isset($_POST['update'])){ //update functionality }
if(isset($_POST['delete'])){ //delete functionality }
?>

php:using two buttons inside same form and form submission

i am designing a login page using html and php.it includes two buttons one for login and another one to redirect to the registration page.i am planning to use php code to validate login data on the same page.here is my code
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<div>
<table style="width: 100%; height: 377px;">
<tr>
<td rowspan="4"><img src="woman.jpg" height="100%" width="100%" alt="woman"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td align="right" style="padding:0px;color:white" >username:</td>
<td><input runat="server" name="Text1" type="text" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td align="right" style="padding:0px;color:white">password</td>
<td><input runat="server" name="Text2" type="text" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td></td>
<td ><input name="Submit1" type="submit" value="login" />
<input onclick="document.location.href='regforswa.php'" name="Submit2" type="submit" value="register" /><br/>
forgot password</td>
</tr>
</table>
</div>
</form>
but the button for registration is not working properly.it is not redirecting to the registration page.i don't want to put register button outside the form.i want to keep the same design and achieve required functionality.
Change the Button-Type from type="submit" to type="button" to disable the form submission:
<input onclick="document.location.href='regforswa.php'" name="Submit2" type="button" value="register" />

change action attribute using Javascript isn't sending to php

Hi try to submit user data from client side to server side by changing action attribute after validation has completed (It is something I did in the past and suddenly it is not working).
My html form(post)
<!-- Submit data to server-side -->
<form id="register" method="post">
<table class="tableReg" border="0">
<tr class="rowReg">
<td>User name:</td>
<td><input onfocus="resetColor('userID')" type="text" id="userID" name="user"></td>
<td id=note1></td>
</tr>
<tr class="rowReg">
<td>Password:</td>
<td>
<input onfocus="resetColor('passwordID1')" type="password" id="passwordID1" name="password1">
</td>
<td id=note2></td>
</tr>
<tr class="rowReg">
<td>Repeat Password:</td>
<td>
<input onfocus="resetColor('passwordID2')" type="password" id="passwordID2" name="password2">
</td>
<td id=note3></td>
</tr>
<tr class="rowReg">
<td>Email:</td>
<td><input onfocus="resetColor('emailID1')" type="email" id="emailID1" name="email1"></td>
<td id=note4></td>
</tr>
<tr class="rowReg">
<td>Repeat Email:</td>
<td><input onfocus="resetColor('emailID2')" type="email" id="emailID2" name="email2"></td>
<td id=note5></td>
</tr>
</table>
<input type="button" onclick="checkInput()" value="Submit">
</form>
If you see after I press the sumbit button the information is checked and if passed validation is being sent to server side, I used javaScript:
function checkInput(){
var regexUser = /^(?=.{1,20}$)[a-zA-Z0-9]+$/;//all letters ad number and size between 1-20
var regextPwd = /^(?=.{8,8}$)(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$/; //Password must contain 8 characters and
// at least one number, one letter and one
// unique character such as !#$%&?
//Email regex
var regexMail = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var errorArr = new Array(0,0,0,0,0);
document.getElementById("register").action = "databasebuilder.php";}
Now after I validate input it suppose to send to databasebuilder.php which is in the same directory on my local host but it doesn't!
I already tested it without and it should be reachable why document.getElementById("register").action is not working.
Why dont you submit the form by yourself. Like this...
document.getElementById("register").action = "databasebuilder.php";
document.getElementById("register").submit();
Try this,
Add this in your js function
document.getElementById("register").action= "databasebuilder.php";
reutrn true;
Also change the type from button to submit like
<input type="submit" onclick="return checkInput()" value="Submit">

Using a function from process.inc.php in a form on another page

I am trying to create a form that has (using the table to do formatting for now.
<form action="addcontent()" method="POST" class="formleft">
<table>
<tr>
<td></td>
<td><input type="hidden" id="id"/></td>
</tr>
<tr>
<td>Page:</td>
<td><input type="text" id="page"/></td>
</tr>
<tr>
<td>Information:</td>
<td><textarea id="info"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" id="submit" value="Add Content">
</tr>
</table>
</form>
how can I use a function called addcontent() within process.inc.php ?
Any ideas?
The action should be filename not function name
action="process.inc.php"
and if you want to call this function than you have to use onSubmit in form tag for calling this function when submitting the form
onSubmit="addcontent()"
For including the content of process.inc.php in other file use below code
include_once("path-to-file/process.inc.php");

Form not submitting html php

My form is not submitting, My database insert class works and I have tested. But my form seems not to want to submit or post values from my form. Is there something I left out?
<form name="form1" method="post" action="sub_newsletter.php" enctype="multipart/form-data">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">Newsletter</td>
</tr>
<tr>
<td>Name : </td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>Email : </td>
<td><input name="email" type="text"></td>
</tr>
<tr>
<td> <input type="text" name="check_spam" id="check_spam" size="30" style="display:none;" /> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="image" name="submit" src="images/sub.jpg" style="width:180px; height:70px;"></td>
</tr>
</form>
</table>
My submit script
<?php
include('includes/database.php');
include('includes/settings.php');
include('includes/newsletter.php');
if (isset($_POST['submit'])){
//to check if posting
echo $username=rtrim($_POST['name']);
echo $myemail=rtrim($_POST['email']);
//
$check=$_POST['check_spam'];
if(!empty($check)){ echo "You are spam"; } else{
$username=rtrim($_POST['name']);
$myemail=rtrim($_POST['email']);
$news = new Newsletter();
$new->first_name=$username;
$new->email=$myemail;
$new->create();
echo "<script>alert(\"Thank you for your subscription\"); </script>";
echo "<script>window.location.replace(\"index.html\"); </script>";
}
}
?>
You obviously missed submit button
<input type="submit" name="submit">
Since you have already a field with that name, just change it or use a different name.

Categories