简体   繁体   中英

Send Javascript array when submitting form with Javascript

So I would like to submit the form using Javascript, and with the form I would like to send also one Javascript array. So far I have this which is not working (I don't get the modified URL and I get the one specified in the action property of the form):

<script>
    function submit_form() {
        var unique_id_to_delete = [];

        if (confirm('Remove records ID: ' + unique_id_to_delete.toString() + '?')) {

            document.getElementById("submit_form").submit(function(event) {
                event.preventDefault();
                var url = 'submit.php?id_delete='+unique_id_to_delete.toString();
                window.location.href = url;
            });

        } else {
            alert('Ok, next time!');
        }
    }
</script>

Form:

<form id="submit_form" method="post" action="submit.php" enctype="multipart/form-data">

<input type="button" onclick="submit_form();" value="Submit"/>
</form>

Form submit using javascript

        function submit_form()
        {

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                  //response after success
                  //xmlhttp.responseText
                }
            }
            xmlhttp.open("GET","YOUR URL WITH PARAMS",true);
            xmlhttp.send();


        }

Heh.. ok I tried this which I think is not what you thought..

<script>
    function submit_form() {
        var unique_id_to_delete = [];

        if (confirm('Remove records ID: ' + unique_id_to_delete.toString() + '?')) {

            var data1 = JSON.stringify(unique_id_to_delete);
            $.ajax({ type: "POST", data: {value1:data1}, url: "submit.php", success: function() {  
                 } });

        } else {
            alert('Ok, next time!');
        }
    }
</script>

script:

function submit_form() {
var unique_id_to_delete = [];
    if (confirm('Remove records ID: ' + unique_id_to_delete.toString() + '?')) {    
            var url = 'submit.php?id_delete='+unique_id_to_delete.toString();
            alert(url);
            window.location.href = url;            
    } else {
        alert('Ok, next time!');
    }
}

Form:

<form id="submit_form" method="post" enctype="multipart/form-data">
<input type="button" onclick="submit_form();" value="Submit"/> 
</form>

Your form will never but submitted as your are simply redirecting the window location, thus creating a GET rather then a POST as intended. You could do:

function submit_form() {
  var unique_id_to_delete = [1,2,3,4,5,6];

  if (confirm('Remove records ID: ' + unique_id_to_delete.toString() + '?')) {

      $.ajax({
          url: "submit.php",
          type: "POST",
          data: unique_id_to_delete,
      });

  } else {
    alert('Ok, next time!');
  }
}

And not need a form:

<input type="button" onclick="submit_form();" value="Submit"/>

I haven't used PHP in a while, but at the other end you need to use json_decode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM