简体   繁体   中英

Getting data from HTML form, and from Javascript array upon form submission

I'm stuck.

I have a regular HTML form that submits to itself.

<form action="<?php print $phpSelf;?>" method="post" id="PO">
  <input>...</>
  <input>...</>
  <input>...</>
  <input type="submit" id="btnCreate" name="btnCreate" value="Create" tabindex="900" class="button">
</form>

And I have an array in Javascript, I'll call it

var jArray;

I need to get the information from the form, and the information from the Javascript array. I can get each of them separately, but I don't know how to get them at the same time.

For the Javascript, this is the method I am using.

function submitPO(){
  $.ajax({
     type:"post",
     url: "jsTo.php", //Send the variable to this page
     data:{partsToAdd: jArray}, //{variable name (POST), data to be passed}
     cache:false,
     success: function(html){ //Function to execute when successful
        console.log("Success in the function");
        $('p#msg').html(html);
     }
  });
return false
}; //End of SubmitPO

<p id = "msg"></p>
<form>
    <input type="submit" value = "submit" onclick = "return submitPO();">
</form>

When I press the button, it sends the array to jsTo.php where I can get it.

$selectedParts = $_POST['partsToAdd'];

That works fine. I use a similar method for getting the information from the HTML form.

$To = htmlentities($_POST["To"], ENT_QUOTES, "UTF-8");

So, like I mentioned above, I can get either the data from the javascript array, or the data from the form, but not both. Anyone able to help me figure this out? I've looked all over SO, and have found tons of resources on how to submit multiple forms with one button, how to post from JS to PHP, etc., but nothing that has touched on this issue.

您可以使用.serialize从表单中获取数据,然后可以使用$.param .serialize将其添加到数组数据中

 data:$.param({partsToAdd: jArray})+'&'+$('#PO').serialize(), 

You can just serialized your form.

in your ajax data, just do

data: $('form').serialize()

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