2010-03-23 9 views
0

Je suis un peu nouveau sur JQuery et je me demandais comment afficher le texte suivant lorsque l'utilisateur soumet des changements <p>Your changes have been saved.</p>? Comment puis-je corriger mon code pour qu'il affiche ce message?Formulaire JQuery Soumettre une question?

Script JQuery.

$(function() { 

$(".save-button").click(function() { 
    $.post($("#contact-form").attr("action"), $("#contact-form").serialize(), function(html) { 
     $("div.contact-info-form").html(html); 
    }); 
    return false; // prevent normal submit 
}); 

}); 

Voici le code HTML.

<div id="contact-info-form" class="form-content"> 

<h2>Contact Information</h2> 
<form method="post" action="index.php" id="contact-form"> 
<fieldset> 
<ul> 
<li><label for="address">Address 1: </label><input type="text" name="address" id="address" size="25" class="input-size" value="<?php if (isset($_POST['address'])) { echo $_POST['address']; } else if(!empty($address)) { echo $address; } ?>" /></li> 
<li><label for="address_two">Address 2: </label><input type="text" name="address_two" id="address_two" size="25" class="input-size" value="<?php if (isset($_POST['address_two'])) { echo $_POST['address_two']; } else if(!empty($address_two)) { echo $address_two; } ?>" /></li> 
<li><label for="city_town">City/Town: </label><input type="text" name="city_town" id="city_town" size="25" class="input-size" value="<?php if (isset($_POST['city_town'])) { echo $_POST['city_town']; } else if(!empty($city_town)) { echo $city_town; } ?>" /></li> 
<li><label for="state_province">State/Province: </label> 
<?php 

echo '<select name="state_province" id="state_province">' . "\n"; 
    foreach($state_options as $option) { 
    if ($option == $state_province) { 
     echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n"; 
    } else { 
     echo '<option value="'. $option . '">' . $option . '</option>'."\n"; 
    } 
    } 
echo '</select>'; 

?> 
</li> 

<li><label for="zipcode">Zip/Post Code: </label><input type="text" name="zipcode" id="zipcode" size="5" class="input-size" value="<?php if (isset($_POST['zipcode'])) { echo $_POST['zipcode']; } else if(!empty($zipcode)) { echo $zipcode; } ?>" /></li> 

<li><label for="country">Country: </label> 
<?php 

echo '<select name="country" id="country">' . "\n"; 
    foreach($countries as $option) { 
    if ($option == $country) { 
     echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n"; 
    } 
    else if($option == "-------------") { 
     echo '<option value="' . $option . '" disabled="disabled">' . $option . '</option>'; 
    } 
    else { 
     echo '<option value="'. $option . '">' . $option . '</option>'."\n"; 
    } 
    } 
echo '</select>'; 

?> 
</li> 

<li><label for="email">Email Address: </label><input type="text" name="email" id="email" size="25" class="input-size" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } else if(!empty($email)) { echo $email; } ?>" /><br /><span>We don't spam or share your email with third parties. We respect your privacy.</span></li> 

<li><p>Changes have been saved</p><input type="submit" name="submit" value="Save Changes" class="save-button" /> 
    <input type="hidden" name="contact_info_submitted" value="true" /> 
<input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li> 
</ul> 
</fieldset> 

</form> 

</div> 

Répondre

2

dans votre fonction de rappel:

$.post($("#contact-form").attr("action"), $("#contact-form").serialize(), function(html) { 
    $("div.contact-info-form").html(html); 
    $('.yourConfirmationDiv').html('<p>Changes saved!</p>'); 
}); 
2

Vous pouvez utiliser le $.ajax qui vous permet plus de gérer des événements comme les erreurs et les délais d'attente.

$.ajax({ 
    url: $("#contact-form").attr("action"), 
    type: "POST", 
    data: $("#contact-form").serialize(), 
    success: function(html){ 
     $("div.contact-info-form").html(html); 
     // Add saved message to DOM 
    } 
    error: function(error){ 
     // Handle error 
    } 
}); 
0

Si vous voulez afficher au-dessus de la forme, faites ceci:

$(function() { 
    $(".save-button").click(function() { 
    $.post($("#contact-form").attr("action"), $("#contact-form").serialize(), 
     function(html) { 
     $("div.contact-info-form") 
       .html("<p>Your changes have been saved.</p>" + html); 
     }); 
     return false; // prevent normal submit 
    }); 
}); 
Questions connexes