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">
Related
I am working on my website, and I am using PHP. I am facing this problem (or maybe not a problem) where I am trying to take data from URL to put them back in the relevant textboxes, for example when someone tries to sign up, and he fails because the passwords don't match. I don't want him to fill the username/email again, I would fill it for him.
This is how the form looks like:
<div class="formie">
<br><table width="350">
<form action="signup.php" method="POST">
<h2><u>.: Registration :.</u></h2>
<br><br>
<tr>
<td width="50%"><b>Username</b></td>
<td width="50%"><input type="text" class="txtbox" name="uid" maxlength="30"></td>
</tr>
<tr>
<td width="50%"><b>Password</b></td>
<td width="50%"><input type="password" name="pwd" maxlength="30" class="txtbox"></td>
</tr>
<tr>
<td width="50%"><b>Confirm Password</b></td>
<td width="50%"><input type="password" name="repeat_pwd" maxlength="30" class="txtbox"></td>
</tr>
<tr>
<td width="50%"><b>E-mail Address</b></td>
<td width="50%"><input type="text" name="emailaddr" maxlength="40" class="txtbox"></td>
</tr>
<tr>
<td width="50%"><input type="submit" name="signup-btn" value="Register" class="btn"></td>
<td width="50%"><input type="reset" value="Clear" class="btn"></td>
</tr>
</form>
</table>
</div>
And I am trying to doing this
if ($pass !== $passrepeat) {
header("Location: register.php?error=passnomatch&uid=".$username."&emailaddr=".$email);
exit();
}
Note that register.php is the form page, and signup.php is just the php where it deals with sql queries and checks and all that.
Any idea why uid=... is not filling out the textboxes ?
Thanks a lot
here is only part of my code, the action does not well ...action. when i view in the google javascript tools i have an id in the action line
when i hit the submit button, nothing happens, its suppose to go to php/form.php with ID 28 in this instance
......
if (mysqli_num_rows($result))
{
while ($row = mysqli_fetch_assoc($result))
{
?>
<form name="form-list" method="post" action='php/form.php?id=<?php echo $row["id"]; ?>' >
<table>
<tr>
<td valign="top">
<label for="surname">Surname</label>
</td>
<td valign="top">
<?php echo $row["title"]; ?>
</td>
</tr>
<tr>
<td valign="top">
<label for="name">Id</label>
</td>
<td valign="top">
<input type="text" value='<?php echo $row["id"]; ?>' name="name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<input type="button" value="Submit" />
</td>
</tr>
<?php } } ?>
</table>
</form> .....
<input type="submit" value="Submit" />
The input type is not button it is submit.
The value attribute just changes the written text on the button but does not change it's behavior.
Note to the other answers:
Using POST is fine if he handles the request in the right way. In the given code example everything except the submit button works and is fine.
In this case the id is transmitted as a GET and all other values are submitted via POST. You can mix them.
Later you do have to use $_GET["id"] and $_POST["name"].. or you can even use $_REQUEST["..."] for all of them.
I have a form like this
<form id= 'frm_request'>
<table class="opt_tbl1" align="center">
<tr>
<td><strong>TLD No.</strong></td>
<td colspan="2"><input type="text" id="tldno" name="tldno" size="25" maxlength="25" />
</td> </tr>
<tr > <td><strong>Name of Worker</strong></td>
<td colspan="2"><input type="text" id="fname" name="fname" size=50 maxlength=50 /></td>
</tr>
<tr>
<td ><strong>Date of Birth (Optional)</strong></td>
<td ><input id="new_day" name="dateofbirth" type="text" />
<tr>
<td colspan="3" align="center"><br/>
<input type="submit" name="_btn" value="Submit" action=""/></td>
</tr>
</table>
</form>
When the focus moves from input box “tldno”, the input “fname” to be filled by getting the html from another domain which I have no control. The html in the other domain, is like this:
<body>
<form method="POST" name="form1" action="../dbms/allindvres.asp" target="I1">
<table >
<tr>
<td width="33%" height="22" align="middle" ><font size=4>TLD No <input name="TLDNO" size="10"></td>
<td width="33%" height="22" align="middle" ><font size=4>CC No <input name="CCNO" size="10"></td>
<td width="33%" height="22" align="middle"><font size=4>or Name <input name="name" size="30"></td>
<td width="33%" height="22" align="middle">
<p align="center">< <input type="submit" value="Get Life Dose" name="B1"></p>
</td>
</tr>
</table>
</form>
<iframe name="I1" >
Your browser does not support inline frames or is currently configured not to display inline frames.</iframe></p></p>
</body>
Upon submitting the above form, the result will be displayed in the iframe. I want the whole html in my page so that I can scrap traverse through the html to get the name of the tldno holder and display it in my input box. Kindly help me to write an ajax call that will return this html
$("#tldno").blur(function(e){
// ajax POST call to the URL http://10.40.64.100/dbms/allindvres.asp with parameter tldno
//Get html
//do something with this html – This part I know.
});
Can any one help with the above method or suggest alternate methods?
Two important points in this question is, (1) I do not have control on the other domain. (2) the URL on the other domain accepts only POST not GET
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>
Here is the form that is confusing me
<h1>
Login
</h1>
<form action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>
Username:
</td>
<td>
<input type="text" name="user" maxlength="30">
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type="password" name="pass" maxlength="30">
</td>
</tr>
<tr>
<td colspan="2" align="left">
<input type="checkbox" name="remember">
<font size="2">
Remember me next time
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" name="sublogin" value="Login">
</td>
</tr>
<tr>
<td colspan="2" align="left">
Join
</td>
</tr>
</table>
</form>
I got the code from this tutorial and it works fine but i cant seem to understand where a/the form submit too if no action is present
If action is set to "" or if the action attribute is missing, the form submits to itself. That is, if your script is index.php, your form submits to index.php.
If the form's action attribute is either set to "" OR not specified, it will still default to action="self", thus the form will send to the address of the document containing the form.
So,
<form method="post">
<!-- IS THE SAME AS... -->
<form action="" method="post">
<!-- IS THE SAME AS... -->
<form action="self" method="post">
(Try it)
For reference, see the HTML standard 4.10.18.3 #8:
http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#form-submission-algorithm
default action for form submission is METHOD="GET" and ACTION="SELF"
you should use a form name
if the action is empty then it posts to itself.