Here are two forms fregister and register. In first form I am taking input of name,username and checking the availability of username by clicking on Check user id button.If username is not available I am displaying message with the help of php and want to show this message to user with the fields he filled before.
<?php RegFormCrAndValid($name,$username,$email) {?>
<form name="fregister" method="post">
<table>
<tr><td>Name</td>
<td><input type="text" id="name" name="name" value ="<?php echo $name;?>" onChange="ValidAllAlpha()" required></td>
<td><span id="ErrorName"></span></td></tr>
<tr><td>Username</td>
<td><input type="text" id="user" name="UsrText" value="<?php echo $user;?>" required onblur="SetNextElement()"></td>
<td><?php echo $_SESSION['ErrorUserMsg']?></td> <td><input type="submit" name="CheckUsr" value="Check User ID"></td> </tr> </form>
In second form, the user will register here first checking the availability and then submission will take place by clicking on register.In this form too if user has not registered successfully then I want to show message with fill fields.
<form name="register" method="post">
<td style="display:none"><input type="text" id="fname" name="fname" required></td>
<td style="display:none"><input type="text" id="fuser" name="fuser" required></td>
<tr><td>Email</td>
<td><input type="email" id="email" name="email" value="<?php echo $email;?>" onChange="ValidateEmail()" required></td>
<td><span id="ErrorEmail"></span></td></tr>
<tr><td><input type="submit" name="submitForm" value="Submit" onClick="ValidationCheckOnSubmit('register');return false;"></td></tr>
</form>
</body>
</html>
Two rows are not displayed here because they are taking the values of name and username from the form fregister with the help of javascript and then displaying them.
JavaScript Codes // validations are there, here I am mentioning the code for passing values to fname and fuser
function SetNextElement()
{
document.getElementById('fname').value = document.getElementById('name').value;
document.getElementById('fuser').value = document.getElementById('user').value;
}
<?php }?> //RegFormCrAndValid($name,$username,$email) this function getting closed over here
php coding
if(isset($_POST['CheckUsr']))
{
if(IsUsernameAvail($_POST['UsrText'])) //IsUsernameAvai checking for the username availabilty return true if available as false
{
$_SESSION['ErrorUserMsg']="<font color=red>This Username Is Available</font>";
}
else
{
$_SESSION['ErrorUserMsg']="<font color=red><--Username is not available</font>";
}
}
if(isset($_POST['submitForm']))
{
if(IsUsernameAvail($_POST['fuser']))
{
echo 'You have been successfully registered';
}
else
{
$_SESSION['ErrorUserMsg']="<font color=red><--Username is not available</font>";
RegFormCrAndValid($_POST['fname'],$_POST['fuser'],$_POST['email']);
//RegFormCrAndValid($name,$username,$email) is the function in which whole html and javascript code is there to create and validate the form.
}
}
else{ if(isset($_POST['CheckUsr']))
{
RegFormCrAndValid($_GET['name'],$_POST['UsrText'],'');
}
else RegFormCrAndValid('','','');
}
My problems
While on clicking CheckUserId button I want the page to stay over there only, why after clicking it doesnt stay on same page with all field fill as they were filled by user. It should just perform the check, why does page reloads?
If I am able to stay over the same page then I dont need to call RegFormCrAndValid($name,$username,$email) this function again to rebuild my form, is it possible to stay there with filled fills and not calling function.
I want to eradicate the use of fake columns to show username and name again because in real registration form there are going to be lot of fields and I cant have fake calling or assignment for all of them.
I dont want to use ajax or jquery, want to achieve everything through javascript,php and html.
When you are clicking on <input type="submit" name="CheckUsr" value="Check User ID"> the form is being submitted, because thats what clicking on submit buttons do, unless you have javascript to block it and do something else.
When a page is being submitted back to the server, the page will reload.
Now, over to your basic goal : you must understand that the actual checking which you are doing to determine whether username is available or not, is on the server side, that is, the logic resides on the server. Whereas, the form which the user is typing the username on, is on the client side, that is residing on the users computer, being displayed through the browser. Now once the user types the name, you somehow need to pass that data over to the server side for it to do the checking and perform actions accordingly. Therefore you have two options:
a) Submit the form as you are now, and use server side php code to collect all the data filled by the user and populate them back again
b) donot submit the form, just make an ajax request to a php script, which will take as input the username and return to you either a true or false response which you can catch using javascript, and accordingly allow the form to be submitted or not submitted.
In that case either on the submit buttons onclick event or the forms onsubmit event trigger set a javascript function to make the ajax request and "return false" if the ajax request returns false or "return true" if the ajax request returns true. Also in the "false case" you can use getElementById to set the error message for the user.
Answer for (a)
You are using form submit action
Default behaviour of form submit is it will reload the page. You have to suppress the default behaviour of form submit by using return false .But it is not recommended.. you can use AJAX
Sample Code
$('#yourFormId').submit(function () {
//business logics here
return false;//to avoid page reload
});
You don't need any more the onclick event on the submit button:
<input class="submit" type="submit" value="Send" />
Related
I have a simple form that collects data and sends it to a PHP script using POST.
<form method="post">
<input type="text" name="cost">
<button name="submit" type="submit">Submit</button>
</form>
The PHP script is,
if(isset($_POST['submit'])){
echo "set";
}
I want to know what happens when I click on the submit button?
The PHP manual says the following about isset,
isset — Determine if a variable is set and is not NULL
When exactly is the submit button SET? When I echo out echo $_POST['submit']; it outputs nothing.
It's only when I use the value attribute along with the submit button that I get something on $_POST['submit'];. Why should I use the value with the submit button? What exactly does it do?
I want to know what happens when I click on the submit button?
It submits the form.
When exactly is the submit button SET?
When the user submit's the form.
When I echo out echo $_POST['submit']; it outputs nothing.
You didn't specified a value for it, so it returns an empty string ($_POST['submit'] === "")
Why should I use the value with the submit button? What exactly does it do?
Well on an button the value is not needed, it is enough when it is set, so you can check if the button was submitted and not an other form f.ex.
Try with this
<form action="" method="post">
<input type="text" name="cost" />
<input type="submit" name="submit" value="Submit" />
</form>
In php side
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
echo "set";
}
It submits the whole form data into targeted location and the GET and POST methods are used to send encoded data to the targeted location
The GET method is restricted to send upto 1024 characters only.
The POST method does not have any restriction on data size to be sent.
I have a PHP page that loads several parts of a form using AJAX. For instance, first check if the user is already registered, if so the script loads (with AJAX) the rest of the form. The form will not be submited using AJAX what can be a problem when the user submits the form (without AJAX) - imagine there are some errors - the form will loose all values.
I'm wondering if CSS hiding part of the form and after the successful login use JS to display the rest of the form, would be better.
Here some code:
<form action="some_action.php">
Email: <input type="text" name="email" id="email"> <br />
Password: <input type="password" name="password" id="password"> <br />
<button id="vrf_login">Verificar</button>
<div id="rest_form">
</div>
</form>
AJAX:
- CHECK login: if email and password matches then
- LOAD the form for div with id "rest_form"
(it is in another file, for instance:
<input type="text" name="place" id="place">
<input type="text" name="age" id="age">
<input type="submit" name="submit" value="submit">
)
The problem is if I submit the form (without AJAX) and there are errors I will loose the form loaded with AJAX
EDIT (again)
Thank you all for your constructive suggestions:
The solution I adopted is close to the first Alkis's suggestion:
almost all the form is hidden (CSS)
after some logic choices the (part of the) form is turned visible (jQuery) - to "remember" what parts should be visible in case of submission failed (server side validation) some session variables hold the information (AJAX) - and then, after the submission (failed) use jQuery to restore the prior form structure (get the session variables with JS this way: var xpto = "<?php echo $_SESSION['prior_xpto']; ?>" ; )
the fields of the form will remember theirs values (with PHP)
You have 3 options.
Stop loading the whole form by ajax. Hide it with css and show it if the the conditions are met. If the page is shown after some validation error, just show it (change the css inline or give it a different class)
Have a condition and every time the page loads check if it is a first load or if the page is shown after some validation error occured. If the latter is true then load again the form with ajax. This condition can be a hidden field that takes its value from the server and you check it on the client every time you serve the page.
The second solution can be done on the server too. Have the condition be checked on the server. If it's a first load, then don't populate the form and let it be populated from ajax as you do now. If it's after a validation error then pre-populate the form. It's just an if/else clause.
Please provide some codes for your question, but i guess your problem is sending result using a button with "submit" type !
if you have a form like this:
<form>
<inputs ...>
<input type="submit" value="Send data" onclick="SendDataUsingAjax()" >
</form>
after clicking on submit all values on input will reset regardless of what your ajax function is doing. to fix this problem you only need to change type="submit" to type="button".
I don't have many instances that PHP craps out on me. But this is surely one of them, I have checked through this for an entire day and figured it might coem from a 3rd party. But a symptom that tells me this error is random is that I cannot even try to set a string variable to post back from the submit. Neither isset nor empty works to capture the event. When I press the submit, the only thing I see is my txtbox variables get shot up into the URL. but NO validation occurs.
So right now now I have a submit button for a login page as follows:
<form action='Login.php' method='post'>
<table align="center">
<tr><td>Username</td></tr><tr>
<td><input type="text" name="txtUsername"/></td></tr>
<tr><td>Password</td></tr><tr>
<td><input type="text" name="txtPass"/></td></tr>
<td colspan="2"><input type="submit" name="btnLogin" ID="btnBlueTemp" value="Sign in!" /></td></tr>
<tr><td class="errorLogin"><?php echo $Error;?></td></tr>
</table>
</form>
I've called the necessary POSTs and here is the event.
$txtUsername = $_POST["txtUsername"];
$txtPass = $_POST["txtPass"];
$btnLogin = $_POST["btnLogin"];
if(isset($btnLogin)){
$Error = "ok";
}
What could be causing a problem that is not letting my button handler be caught? I am using Internet Explorer 9.
Do not use submit button to check that the form has been submited. either add a hidden form field and check that if its set, or use some other mechanism. You will not have this problem in firefox, but in IE you will.
You are doing it wrong. When you use $btnLogin = $_POST["btnLogin"]; , you are actually setting the variable. So your script will get executed even you hit the url from address bar without hitting submit button.
Instead use -
if(isset($_POST['btnLogin']){
// do stuff
}else{
// do other stuff
}
Below is my code for a simple form in the create_session.php page. I am using the forma action method to navigate the user to the "QandATable.php" page when the user submits the form. But what I want is that if the user types in the number 1 in the number of sessions textbox, then navigate to the QandATable.php page wheh the user submits the form, else if it is any other number, then when the user submits the form, I want it to go back to the "create_session.php" (Back to its own page). Imagine it like you click on the submit button and it refreshes the page so it goes back to being a blank form, thats what I want to do if the number of sessions textbox contains a number which is bigger than '1'. How can this be done? I am using php and jquery code as well as basic html.
Thank You
<form action="QandATable.php" method="post" id="sessionForm">
<p><strong>Number of Sessions you Require:</strong> <input type="text" id="sessionNo" name="sessionNum" onkeypress="return isNumberKey(event)" maxlength="5" /></p>
<p><input class="questionBtn" type="submit" value="Prepare Questions" name="prequestion" onClick="myClickHandler(); return false;"/></p> <!-- Prepare Questions here-->
</form>
At the very top of the QandATable.php script add:
if ($_POST['sessionNum']!=1) {
header("Location: create_session.php");
exit();
}
But as others have mentioned in the comments, you might as well just make the form submit to the create_session.php page and if it's successful then redirect to the next page.
i am designing a register form for a site.in this form in html forms we have to password type
and a submit button.
<form action="index1.php" method="post">
<b>pass:</b> <input type="password" name="pass" size="25"/>
<b>pass:</b> <input type="password" name="cpass" size="25"/>
<input type="submit" name ="submit" value="comfirm">
on of them are for pass and second for confirm password and a button for submit.
user must fill both of pass and cpass fields until program can store in database
i want that when user click on submit button (for speed up my program) my program check that if two fields are filled are no.if they have filled connect to database and store them into database or if no show a alert to user and doesn't connect to database.
my database codes are in index1.php.
if it possible i check them with javascript functions with onclick in submit?
(i could retrieve form values into a javascript functions and checked them but could not stop program and so it go to index1.php and connect to database that i don't need)
if yes how?
if no how can i do that.
if it possible in javascript me go to a particular page in php for example 5.php?
Use the onsubmit event on the form element:
<form action="..." onsubmit="return validate_fields();">
The return keyword is important here. Inside your validate_fields, return false if the check fails or true if it succeeds:
function validate_fields() {
...
if(fields_is_valid) {
return true;
}else{
alert('Fill in all fields!');
return false;
}
...
}
This will stop the form from being sent if the validation fails.