Jump to content

Post Checked Values To Mysql Jquery Serialize()


smithdp1

Recommended Posts

Hi, I am trying to post checked off items to a mysql table using php and jQuery serialize(). I have the form made with array as name but I dont know where to go now.

 

Here is my form:

 

<form name="myform">
<input type="checkbox" name="clientId[]" value="10"> - Fred <br />
<input type="checkbox" name="clientId[]" value="20"> - Mary <br />
<input type="checkbox" name="clientId[]" value="30"> - Bob <br />
<input type="hidden" name="campaignId" value="2">
<input name="submit" type="button" value="Submit">
</form>

 

MySql table

id - auto increment

clientId

campaignId

 

If I check off Fred and Mary for instance I want the clientId and campaignId to post to above table for only Fred and Mary.

I just told you. If you don't know what to do with what I posted, look it up in the manual. Seems like your trying to run before you can walk.

 

I'll give you a head start

if ($_SERVER['REQUEST_METHOD'] == "post") {
print_r($_POST);
}
<form name="myform" method="post">
<input type="checkbox" name="clientId[]" value="10"> - Fred <br />
<input type="checkbox" name="clientId[]" value="20"> - Mary <br />
<input type="checkbox" name="clientId[]" value="30"> - Bob <br />
<input type="hidden" name="campaignId" value="2">
<input name="submit" type="button" value="Submit">
</form>

 

This will show you what info you have to work with once you hit submit.

I just tried what you have posted and it does nothing at all when submited. Is you if statement php or javascript? I have tried putting it in both tags and still does nothing. Inkow that after the form is submited I need to do something like:

 

clientid = $_POST($clientId);

campaignId = $_POST($campaignId);

$data = array(){

clientId,

campaignId

}

 

But I am not sure on the javascript and exact syntax. I was just asking for some help.

Here's pretty much what he was showing you. This is a working version.

Demo: http://xaotique.no-ip.org/tmp/33/

 

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST")
{
   echo '<pre>', print_r($_POST), '</pre>';
}

?>

<form name="myform" method="post" action="">
   <input type="checkbox" name="clientId[]" value="10"> - Fred <br />
   <input type="checkbox" name="clientId[]" value="20"> - Mary <br />
   <input type="checkbox" name="clientId[]" value="30"> - Bob <br />
   <input type="hidden" name="campaignId" value="2">
   <input name="submit" type="submit" value="Submit">
</form>

Not pretty much, that's the exact same as I posted.

Pretty much as in changing the "post" to "POST" so it would trigger, and encasing it in PRE tags so it would be more readable, then adding the PHP tags so it worked, as well as changing the submit button to an actual submit button.

Pretty much as in changing the "post" to "POST" so it would trigger,

 

That doesn't matter if he uses 'post' in the method. It just has to match.

 

encasing it in PRE tags so it would be more readable

 

That doesn't make anything more functional or more correct

 

then adding the PHP tags so it worked

 

My mistake, I assumed since he was on a php forum... ::)

right i just fixed the button and it works. So now I have an array of data being submited and I need to insert that into db. so I do something like:

 

$data = array(){

$clientid = $_POST($clientId),

$campaignId = $_POST($campaignId)

foreach($clientId as ?){

????now I am lost

By the way thanks for taking the time to help...Happy Holidays!

You're welcome, and sorry for overlooking your last question. Google SQL INSERT and you'll get examples and explanations of how to put it into the database.

Did you post the form? Are your variable correct? $clientId and $campaignId have to be some string. If you meant for it to be a string and not a variable, it would be 'clientId' => $_POST['clientId'], and the same with the next.

thanks Xaotique. So now I put this:

The forms data<br>
<pre>
<?php
$data = array(
    'clientId'   => $_POST['clientId'],
    'campaignId' => $_POST['campaignId']
);
print_r($data);
?>
</pre>

 

And i get this:

The forms data

Array

(

[clientId] => Array

(

[0] => 10

[1] => 20

[2] => 30

)

 

[campaignId] => Array

(

[0] => 2

)

 

)

should I have a campaignId for each key of the clientId?

I don't know because I have no idea what you're actually doing. You currently just have campaignId as a hidden field, and only one in the form. So your result was the correct amount posted.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.