question about php and ajax-jquery
i have a problem in a form. I have a simple form and the validation in a php. All works fine. Now i pass the file in a ajax load() method and appears the problem.
When i press submit the page don't show any massage, simple redirect to the initial page of the website.
i tested and the messages are shown in a separate mode without the ajax. Just using form.php
in the index.php
$("#replace").load("form.php");
in form.php
<?php
require_once("validation.php");
?>
<div id="container">
<form method="post" id="customForm" action="">
<div>
<label for="name">Name</label> <input id="name" name="name"
type="text" /> <span id="nameInfo">Insira o seu nome</span>
</div>
<div>
<label for="email">E-mail</label> <input id="email" name="email"
type="text" /> <span id="emailInfo">Insira um email válido por
favor!</span>
</div>
<div>
<label for="myPassword">Password</label> <input id="myPassword"
name="myPassword" type="password" /> <span id="myPasswordInfo">Insira
pelo menos 4 letras e sem espaços</开发者_StackOverflow中文版span>
<div id="bar"></div>
</div>
<div>
<label for="pass2">Confirm Password</label> <input id="pass2"
name="pass2" type="password" /> <span id="pass2Info">Confirme a
password</span>
</div>
<div></div>
<div>
<input id="send" name="send" type="submit" value="Send" />
</div>
</form>
<?if( isset($_POST['send']) && (!validateName($_POST['name']) || !validateEmail($_POST['email'])
|| !validatePasswords($_POST['myPassword'], $_POST['pass2']))):?>
<div id="error">
<ul>
<?if(!validateName($_POST['name'])):?>
<li><strong>Invalid Name:</strong> Nome muito pequeno</li>
<?endif?>
<?if(!validateEmail($_POST['email'])):?>
<li><strong>Invalid E-mail:</strong> Escreva um email válido por favor</li>
<?endif?>
<?if(!validatePasswords($_POST['myPassword'], $_POST['pass2'])):?>
<li><strong>Passwords inválidas:</strong> Passwords não coicindem ou são inválidas!</li>
<?endif?>
</ul>
</div>
<?elseif(isset($_POST['send'])):?>
<div id="error" class="valid">
<ul>
<?if(!inserirDados($_POST['name'],$_POST['email'], $_POST['myPassword'], $_POST['pass2'] )):?><?endif?>
</ul>
</div>
<?endif?>
</div>
how i can fix this?
My guess is a problem with your submit handler. Two points:
If you're reloading your form every time with load(), you will need to re-attach your submit() handler when it finishes loading (uses the second parameter (callback) to re-attach the handler)
You must either
return false
or calle.PreventDefault()
in your submit handler to prevent the default behaviour of the submit button (which is to, well, submit the form).
Also, note: the reason it reloads the current page is because action="" (which means post to the current url).. and is a strong indicator that your submit handler doesn't prevent normal submission of the form
Edit: You attach a "submit" handler to the form to perform custom ajax stuff instead of the default behaviour. See the jquery doc and this tutorial.
精彩评论