blackandorangecat blackandorangecat - 1 year ago 42
HTML Question

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 type="submit" id="btnCreate" name="btnCreate" value="Create" tabindex="900" class="button">

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(){
url: "jsTo.php", //Send the variable to this page
data:{partsToAdd: jArray}, //{variable name (POST), data to be passed}
success: function(html){ //Function to execute when successful
console.log("Success in the function");
return false
}; //End of SubmitPO

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

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.

Answer Source

You can get the data from the form with .serialize then you can add it to the array data with $.param

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