stop jquery to run server side code on validation fail - php

Sorry for the very basic question but i am asking here after trying to search alot.
Basically i am doing client side validation its running the half code but it is running the php code also even if it fails.
<script type="text/javascript">
function formValidation(){
if($("#inputCname").val() == ''){
alert("Category missing");
($this).css("border-color","red");
return false;
}
return true;
};
</script>
Below i am calling the function. Please note that i have already tried with onclick event on the submit button still not working.
<form class="form form-search" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" onsubmit="formValidation();">
Thanks alot.

You have to add a return to the onsubmit to stop the submission
<form class="form form-search" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" onsubmit="return formValidation();">
EDIT
There's also a problem in your JS function
$("#inputCname").css("border-color","red");

Instead of doing this for every form element, use jquery validation, It will do it all for you. Also showing alert for every form element is not a good practice. It will solve your all problems with validation providing you more features.
You can download jquery validation and check out its documentation from following link.
Download link http://jqueryvalidation.org/
Documentation to implement http://jqueryvalidation.org/documentation/

Related

Submit button redirecting to different address

So the submit button of this form is supposed to send the data to localhost/mvc/contact/test and redirect to localhost/mvc/contact
at the first click of submit button it works well
Right
but when I click the submit button again its taking me to localhost/mvc/test
Not right
here is the html code of the form <form action="test" method="post" accept-charset="utf-8" class="contact">
when I had this as <form action="contact/test" method="post" accept-charset="utf-8" class="contact"> it first redirected me to localhost/mvc/contact but on second attempt redirected to localhost/mvc/contact/contact/test
Btw it's a simple mvc framework you may check it out on github for more clarity on Leggera # Github
The above mentioned code is present on view/contact/index.php
You can try these things:
You must give the full path to the file like <form action="contact/test/index.html" method="post" accept-charset="utf-8" class="contact">
In the index file of contact/test you can use <?php header("Location: http://www.example.com/");?> for redirection.
Maybe you have given wrong paths to files.
You must give more details for more specific answer

Navigate to form after submission

This might seem a simple problem, but im stuck on this one. Hope anyone can help me on this
I'm working on server side form validation in PHP. Everything is working as expected as far as validation goes. But if an error is shown on input or the form gets submitted the browser navigates to the top of the page. How can I prevent this behaviour? I need the page where it is after I click the submit button
<?php include('process_form.php'); ?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form-row">
<div class="col form-group">
<label>Primeiro nome</label>
<input type="text" class="form-control" title = "Inserir nome" name="firstname" value="<?php echo $firstname ?>">
<span class="error"><?php echo $firstnameErr ?></span>
If I understand your question, you are asking quite a lot from just HTML and PHP. Remember that once the form is submitted, the browser navigates away from the current page and loads the form action page (in your case, it reloads the current page as per the directive action="<?php $_SERVER['PHP_SELF']; ?>".
So, how would you position the page at exactly the desired location if there was no form submission going on? That is how you would do it in this case. So, as suggested in the comments, you could modify your action directive: action="<?php $_SERVER['PHP_SELF']; ?>#id_of_form_container". For example, if your form is in a div structure like this:
<div id="contact_form_div">
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
then your action tag would be:
action="<?php $_SERVER['PHP_SELF']; ?>#contact_form_div"
Alternately, you can do some basic form validity testing on the javascript side, during the form submit process. If, for example, a required field is blank, you can return false; - which will stop the submit process and return control to the user.
Here is an example of what basic javascript field validation looks like. And here is an example of using javascript/jQuery to interrupt the form submit process to perform that validation, and return control to the user (via return false;) if validation fails.
References:
MDN article on form validation - note the SmashingMagazine links at bottom
TutorialsPoint - more concise example of the same
Video tutorial of same (30 min)

PHP Formatting URL within a Form Action

I would like to add the ID of a User to the form action as a URL.
For Example view_member.php?id=2
Can somebody possible show me the correct way to format PHP code in the form action below where I can include the id as $recptid
$recptid = $_GET['id'];
I have tried
<form action="<?phpview_member.php?id=$recptid?>" method="post">
I would like to be able to add a link but my formatting is wrong
// insert php code as URL in the form action the a submit is pressed
<form action="<?php?>" method="post">
</form>
Thanks in advance for any help
try this,
<form action="<?php echo "view_member.php?id=".$recptid; ?>" method="post">
i hope it will be helpful.

Form submit to run javascript and then post to php script

I am currently working on a php/html/javascript project. I have a form where when the user presses the submit button, it should run a javascript function and then afterwards post to a php page to process the data that was submitted in the form.
I have the form run a javascript method as below
<form class="form" id="addImageForm" name="addImageForm" action="javascript:validateAddImage();" method="post">
The method validates the form, calls another method and then it submits the form using document.myForm.submit();
How do I get it to submit the form to another php page to process the data including upload selected files.
Thanks for any help you can provide.
Set yout 'action' parameter to your PHP script, and do any javascript procesing in a javascript event.
<form class="form" id="addImageForm" name="addImageForm" action="processing.php" method="post" onsubmit="return validateAddImage();">
Then in your js validation, return 'true' if everything's fine, or 'false' if not. Returning 'true' means 'continue with the submit process and send over data to processing.php
What you need is onsubmit.
<form class="form" id="addImageForm" name="addImageForm" action="addImage.php" onsubmit="return validateAddImage();" method="post">
Replace the action attribute with the name of your PHP page, and have the validateAddImage method as an onsubmit event.
Change the action attribute to the address of the php script you're posting to.
Call the javascript function in an onsubmit attribute instead of action. But you'll have to prefix it with a return statement.
<form class="form" method="post" action="your/script.php" id="addImageForm" name="addImageForm" onsubmit=return javascript:validateAddImage()>
That way if validateAddImage() returns false, the form won't be submitted. But if it returns true it will.

<form> is not working with mootools

I have following code in my php file
<?php
if(isset($_POST['updatebtn']))
{
mysql_query("update table......");
}
?>
<script type="text/javascript">
window.addEvent('domready', function(){
new FormCheck('myform');
});
</script>
<form name="myform" method="post" id="myform" action="">
<input type="text" class="validate['required']" />
<input type="submit" value="updatebtn" />
</form>
as you can see i have added mootools form validation in my file. mootools form validation work only if i add id="myform" in my form. but if i add that (i. e. id="myform"), it is creating problem, means it is not executing update query that i have written at top.
if i remove id="myform" from my form tag, it is executing that update query
do anyone have any idea??
The javascript would validate the input and prevent the form to submitting so it's normal that your server side script won't run.
I think you should make a difference between things run on server side, such as PHP and things run on client side, i.e. javascript.

Categories