$.post in script doesn't work
I need to add new several elements to the page on button click. I don`t understand - when I do everything step by step with fireBug - all is OK. If I switch Firebug off - nothing is added. This is js code:
function Appending()
{
var id = document.getElementById("default-id").value;
id++;
var result = $.post("addForm.php",{idEl:id},"html");
$('#vrWrapper').append(result.responseText);
document.getElementById("default-id").value = id;
}
function removeInput(id) {
$("#" + id).remove();
}
And here is PHP code (addForm.php):
<?php
$id=$_POST["idEl"];
echo '<div class="data" id="'.$id.'"><div class ="userdata"><div class = "input"> <label for="lastName">Last Name:</label> <input id="lastName" type="text"/></div><div class = "input"> <label for="firstName">First Name:</label> <input id="firstName" type="text" /></div><div class = "input"> <label for="city">City:</label> <input id="city" type="text"/></div><div class = "input"> <label for="state">State:</label> <select name="state"><option value="AL">Alabama</option><option value="AK">Alaska</option><option value="AZ">Arizona</option><option value="AR">Arkansas</option><option value="CA">California</option><option value="CO">Colorado</option><option value="CT">Connecticut</option><option value="DE">Delaware</option><option value="DC">District of Columbia</option><option value="FL">Florida</option><option value="GA">Georgia</option><option value="HI">Hawaii</option><option value="ID">Idaho</option><option value="IL">Illinois</option><option value="IN">Indiana</option><option value="IA">Iowa</option><option value="KS">Kansas</option><option value="KY">Kentucky</option><option value="LA">Louisiana</option><option value="ME">Maine</option><option value="MD">Maryland</option><option value="MA">Massachusetts</option><option value="MI">Michigan</option><option value="MN">Minnesota</option><option value="MS">Mississippi</option><option value="MO">Missouri</option><option value="MT">Montana</option><option value="NE">Nebraska</option><option value="NV">Nevada</option><option value="NH">New Hampshire</option><option value="NJ">New Jersey</option><option value="NM">New Mexico</option><option value="NY">New York</option><option value="NC">North Carolina</option><option value="ND">North Dakota</option><option value="OH">Ohio</option><option value="OK">Oklahoma</option><option value="OR">Oregon</option><option value="PA">Pennsylvania</option><option value="RI">Rhode Island</option><option value="SC">South Carolina</option><option value="SD">South Dakota</option><option value="TN">Tennessee</option><option value="TX">Texas</option><option value="UT">Utah</option><option value="VT">Vermont</option><option value="VA">Virginia</option><option value="WA">Washington</option><option value="WV">West Virginia</option><option value="WI">Wisconsin</option><option value="WY">Wyoming</option></select></div></div><div class ="workdata"><div class = "input"><input id="ch1" type="checkbox" class="check" /><label for="ch1">Немирович (бригадир)</label></div><div class = "input"><input id="ch2" 开发者_高级运维type="checkbox" class="check" /><label for="ch1">Немирович (бригадир)</label></div><div class = "input"><input id="ch3" type="checkbox" class="check" /><label for="ch1">Немирович (бригадир)</label></div><div class = "input"><input id="ch4" type="checkbox" class="check" /><label for="ch1">Немирович (бригадир)</label></div><div class = "input"><input id="ch1" type="checkbox" class="check" /><label for="ch1">Немирович (бригадир)</label></div></div><div class ="buttons"><button type="button" id="addAdult" onClick="Appending()">Add Adult</button><button type="button" id="register">Register</button><button type="button" id="cancel" onClick = "removeInput('.$id.')">Cancel</button></div></div>';
?>
Really strange that everything is correct - FireBug shows no mistakes
Here you can see it in action: http://tvorchestva.net/test/form/form.html
Put your code in a callback to post
.
function Appending()
{
var el = document.getElementById("default-id"); // cache the selection
var id = el.value;
id++;
el.value = id;
$.post("addForm.php",{idEl:id},function( htm ) {
$('#vrWrapper').append( htm ); // <-- run after response is received
},"html");
}
You can use a different form in newer versions of jQuery because of Differeds.
Based on example from docs:
$.post("example.php", function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
The problem is that $.post is expecting a function to call on success as the third parameter, but you are passing "html".
Try replacing
$.post("addForm.php",{idEl:id},"html");
with
$.post("addForm.php", {idEl:id}, function(d) {
$('#vrWrapper').append( d );
}); );
As the jQuery documentation points out, "most implementations will specify a success handler;" in their example, they "fetch the requested HTML snippet and insert it on the page," which is exactly what you are trying to do.
try this one
function Appending() {
var id = document.getElementById("default-id").value;
id++;
$.post("addForm.php", {
idEl: id
}, function(html) {
$('#vrWrapper').append(html);
});
document.getElementById("default-id").value = id;
}
function removeInput(id) {
$("#" + id).remove();
}
精彩评论