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. Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/ 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 Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400395 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 Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400402 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 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. Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400405 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. Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400415 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... Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400421 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. Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400422 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> Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400424 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 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 Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400431 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. Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400434 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 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... Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400437 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 Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400439 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/ Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400442 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! Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400443 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. Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400445 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; } Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400448 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" ); Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400451 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) ); Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400456 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] ); Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400459 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); Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400462 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. Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400463 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 ) ) Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400474 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? Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400475 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. (: Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400476 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. Link to comment https://forums.phpfreaks.com/topic/272188-post-checked-values-to-mysql-jquery-serialize/#findComment-1400478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.