how to send form data through javascript?
HTML code-
<form>
....
<a class="art-button" href="edit.php" >Edit</a>
<a class="art-button" href="#" onclick="show_confirm()">Delete</a>
</form>
JavaScript-
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Do you want to delete?");
if (r==true)
{
// call "delete.php?id=value"
}
else
{
//go back t开发者_如何转开发o same page
}
}
how can I call delete.php?id=value
from javascript`
Not sure what you are trying to do but the code should be like:
<a class="art-button" href="delete.php?id=value" onclick="return confirm('Are you sure you want to delete this?')">Delete</a>
no need to submit the form.
Use .submit()
as HS suggests.
Alternatively, if you want to make this work even when the user has JavaScript turned off (that's not necessarily the case - your decision) make the two links submit buttons:
<button type="submit" value="Delete">
and add on_confirm()
to the form's submit event:
<form onsubmit="return on_confirm()">
and make the function return false
or true
depending on the outcome of the confirm():
var r=confirm("Do you want to delete?");
return r;
In your javascript:
(document.forms['formname'] || document.formname).submit();
Give the and id="formId" (<form id="formId">
)
document.getElementById("formId").submit();
For this to work the form needs the url it should submit to.
<form id="myform" method="POST" action="delete.php?id=value">
Edit
If you just want a confirm button i'd use pekkas answer (+1)
Edit in reply to question
You could to:
form = document.getElementById("formId")
form.action = "edit.php?id=value";
form.submit();
if you want it to submit to different pages.
if (r==true) {
$.ajax({
type: "POST",
url: "delete.php",
cache : false,
data: $("#id_of_form").serialize(), // here will be the values from form
success: function(msg){
}
});
and in delete.php you will receive values in $_POST array
With jQuery:
<form id="randomForm">
....
<a class="art-button" href="edit.php" >Edit</a>
<a class="art-button" id="deleteLink" href="#" onclick="show_confirm()">Delete</a>
</form>
<script type="text/javascript">
$("#deleteLink").click(function(){
$("randomForm").submit()
});
</script>
Please note the extra ID tags I have added
window.location.href = "delete.php?id=" + value;
精彩评论