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.

Link to comment
Share on other sites

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.

Edited by TOA
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

Here's pretty much what he was showing you

 

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

 

Your button type needs to be set to submit. Right now, it's just a button.

 

Good call

Edited by TOA
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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... ::)

Edited by TOA
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

)

 

)

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.