smithdp1 Posted December 19, 2012 Share Posted December 19, 2012 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. Quote Link to comment Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 print_r($_POST) will tell you what you have when you hit submit. To go from there, you'd need to post some relevant code Quote Link to comment Share on other sites More sharing options...
smithdp1 Posted December 19, 2012 Author Share Posted December 19, 2012 That is why I am asking for help...i am not sure what to do Quote Link to comment Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 (edited) 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 December 19, 2012 by TOA Quote Link to comment Share on other sites More sharing options...
smithdp1 Posted December 19, 2012 Author Share Posted December 19, 2012 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. Quote Link to comment Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 Is you if statement php or javascript? Well, this is a php forum... Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 Your button type needs to be set to submit. Right now, it's just a button. Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 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> Quote Link to comment Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 (edited) 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 December 19, 2012 by TOA Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 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. Quote Link to comment Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 (edited) 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 December 19, 2012 by TOA Quote Link to comment Share on other sites More sharing options...
smithdp1 Posted December 19, 2012 Author Share Posted December 19, 2012 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 Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 That doesn't matter if he uses 'post' in the method. It just has to match. It does matter. Go ahead and test it. Here's the same thing with "post" instead of "POST".http://xaotique.no-ip.org/tmp/34/ Quote Link to comment Share on other sites More sharing options...
smithdp1 Posted December 19, 2012 Author Share Posted December 19, 2012 By the way thanks for taking the time to help...Happy Holidays! Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 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. Quote Link to comment Share on other sites More sharing options...
smithdp1 Posted December 19, 2012 Author Share Posted December 19, 2012 so is code correct above: $data = array(){ $clientid = $_POST($clientId), $campaignId = $_POST($campaignId) foreach($clientId as ?){ insert the data to table endforeach; } Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 No. You're trying to set an array with keys, correct? $data = array( 'key' => "value", 'key2' => "next value" ); Quote Link to comment Share on other sites More sharing options...
smithdp1 Posted December 19, 2012 Author Share Posted December 19, 2012 $data = array( $clientid = $_POST($clientId), $campaignId = $_POST($campaignId) ); Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 $_POST is an array. You access what's inside it by its keys. $array['key'] $data = array( $clientid => $_POST[$clientid], $campaingId => $_POST[$campaignId] ); Quote Link to comment Share on other sites More sharing options...
smithdp1 Posted December 19, 2012 Author Share Posted December 19, 2012 ok so I put this in my php page and it prints nothing: $data = array( $clientId => $_POST[$clientId], $campaingId => $_POST[$campaignId] ); print_r($data); Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 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. Quote Link to comment Share on other sites More sharing options...
smithdp1 Posted December 19, 2012 Author Share Posted December 19, 2012 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 ) ) Quote Link to comment Share on other sites More sharing options...
smithdp1 Posted December 19, 2012 Author Share Posted December 19, 2012 should I have a campaignId for each key of the clientId? Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 I'm not sure of the reason for this though, as you could just as easily use $_POST['clientId'] anywhere like you would $data['clientId'], but glad ya got it. (: Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.