Question:
How can I use php to check my html form before submitting it?
anonymous
2013-10-28 23:04:27 UTC
I have a basic javascript file for this purpose that works great, but I want to do php instead since javascript can be changed from the client side. I have a separate php file to show an alert box but it takes me to a separate page and displays the alert.
Is there a way to stay on the same page and display the alert and stop the form from submitting with php?
Four answers:
aaa
2013-10-28 23:45:39 UTC
you want php to validate your form, without ever submitting the form. How would the server side php code ever get a hold of your Request variables if you don't submit?



The only way you can call php from a page without submitting a form is to use ajax. Here is an example using jquery:



var tbVal = jQuery("#tb1").val();

jQuery.ajax({

url: "MyPage.php",

type: "POST",

data: "AjaxMethod=ValidateForm&MyData=" + tbVal,

dataType: "text",

success: function(s){

//check value of s, and handle form submission accordingly

if (s == "SUCCESS"){ /*submit form*/ }

else{ /*don't submit form*/ }

},

error: function(e){

//prevent form from submitting

}

});



you can call the above code on form submission.....

and then in your page code:






if (isset($_REQUEST["AjaxMethod"]) && $_REQUEST["AjaxMethod"] == "ValidateForm"){

//check if length is greater than three

if (strlen($_REQUEST["MyData"]) >= 3)

echo "SUCCESS";

else

echo "FAIL";

return;

}



?>



edit: sorry for the lack of indentation but yahoo answers removes the tabs and all whitespace characters in front of a line
Neville
2013-11-04 13:30:31 UTC
No matter what you do, finally you need to use javascript do show the alret and stop the form. The only option is to keep your js code as it is. Since you dont want any client side changes to effect the submitting, In the php page to which the form is submitted, write a code to check all post variables before the rest of the code proceeds. If you however want to use php to check, you can look into the concept of ajax. http://www.w3schools.com/ajax/default.asp
Jeff P
2013-10-29 13:01:39 UTC
There's really two ways you could do this. You could do it as the previous answerers said and use AJAX to submit your data to the PHP page and return data back. Or you could make your form the PHP page and simply post back to the page. Example:




if(isset($_POST))

{

//Do your error checking here. Create a variable called $errors to store errors

}



if(isset($_POST) and !isset($errors))

{

//Do your primary function here: insert into database, send email, redirect user, set cookie, etc.

}

else

{

//display your form here

//also display contents of $errors so the user can see what the errors are.

}

?>



It really depends on what you want to do.
just "JR"
2013-10-29 07:33:45 UTC
AJAX would be the only way if you don't want to submit the form! (in which cas, you don't even NEED a form: you check each field under AJAX, calling a check on "onblur()" on every ).

I do this, for example, on registration forms: when the user has entered is choosen username, I call AJAX to see if the name does NOT exist in the DB, and display an error message if it exists.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...