nectar
nectar

Reputation: 9679

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 to same page
      }
}

how can I call delete.php?id=value from javascript`

Upvotes: 2

Views: 1181

Answers (7)

dmitko
dmitko

Reputation: 2657

window.location.href = "delete.php?id=" + value;

Upvotes: 0

Skippy
Skippy

Reputation: 346

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

Upvotes: 0

Gatman
Gatman

Reputation: 594

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

Upvotes: 0

Ankit Jaiswal
Ankit Jaiswal

Reputation: 23427

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.

Upvotes: 2

edorian
edorian

Reputation: 38961

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.

Upvotes: 0

Pekka
Pekka

Reputation: 449435

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;

Upvotes: 1

HS.
HS.

Reputation: 15710

In your javascript:

(document.forms['formname'] || document.formname).submit();

Upvotes: 0

Related Questions