I’m really new at javascript and JQuery and I’m using JQuery datatables. I have read the page for 100 times and compared the html source and I managed to figure out how to pass the only checked fields into the sData array… Anyway…
I have this array:
var sData
which alerts me which rows I have selected when I submit the form, it gives something like this:
1=1&2=2&10=10&18=18
Before this I used simple table with each row a button which reserved me the one I choose, so this step is really confusing for me…
I used this query to add them to the database:
if (isset($_POST['addtolist']) )
{
$itemid=mysql_real_escape_string($_POST['itemid']);
$AddToListQuery = "
INSERT INTO
reservelist
(itemid, projectid)
VALUES
('$itemid', '" . mysql_real_escape_string($_SESSION['projectidtoreserve']) . "')
";
mysql_query($AddToListQuery, $conn);
header('Location: pm_reserving.php');
exit();
}
And it executed every time after clicking on a button at the specified table row.
But now I have no clue how to pass this sData array to the query…
I can’t just use this sData like it now is to enter it into the database, because it wouldn’t make any sense :/… What do I have to do with this sData?
EDIT
My JQuery code now:
$('#form').submit( function() {
var sData = $('input', oTable.fnGetNodes()).serialize();
alert( "The following data would have been submitted to the server: \n\n"+sData );
} );
oTable = $('#table1').dataTable();
My form:
<form id="form" action="" method="post">
<button type=submit name=addtolist>Button</button>
When I submit the form, I receive the alert mentioned above (but now with id1=1&id2=2 etc.).
PHP:
if (isset($_POST['addtolist']) )
{
$x=1;
$values=array('+sData');
while(isset($_POST['id'.$x]))
{
$values[$x-1]=$_POST['id'.$x];
$AddToListQuery = "
INSERT INTO
reservelist
(itemid, projectid)
VALUES
('". mysql_real_escape_string($_POST['id'.$x]) ."', '" . mysql_real_escape_string($_SESSION['projectidtoreserve']) . "')
";
$x++;
}
mysql_query($AddToListQuery, $conn);
header('Location: pm_reserving.php');
exit();
}
This PHP loop is probably a big fail…But I need this sData var to put into the PHP variable… Oh god I’m so lame at this.
if you are passing
sDatausing GET or POST, the way to recover it is like this:If the URL is (for example in GET):
www.url.com?key=value&key2=value2In PHP you read it as:
So in your example it would be:
As you can see it is not ideal, cause you need to know the id to read the id… a much better format for
sDatawould be:That you would read:
An easy way to read it would be:
EDIT: How all this works if you are submining a form.
If you just submit this, the variables
id1andid2will be submited using POST toexample.php. Now this PHP should do this:example.php
This will work even if you use more variables if they are all named id+number, where number keeps increasing by 1 in each new variable (id1, id2, id3, id4 ….)
After this the $values array cointains all the variables passed! you can use them in query or wherever you need.