Jump to content

extracting values from an array


dazz_club
 Share

Recommended Posts

Hi people,I have a form that features some checkboxes, set up like this---------------------------<input type="checkbox" value="bedroomAndNusery" name="product_interest[]" id="bedroomAndNusery" checked="checked" tabindex="5"/><input type="checkbox" value="bathSafety" name="product_interest[]" id="bathSafety" tabindex="6"/><input type="checkbox" value="feedingSpoon" name="product_interest[]" id="feedingSpoon" tabindex="7"/><input type="checkbox" value="uvSafety" name="product_interest[]" id="uvSafety" tabindex="8"/><input type="checkbox" value="hotWarningLabel" name="product_interest[]" id="hotWarningLabel" tabindex="9"/>---------------------------I would like to capture all this and insert it into my table...so far my script has managed to capture the data and then echo it out (for testing purposes)------------------------if(is_array($_POST['product_interest'])) { echo 'You selected: <br />'; foreach ($_POST['product_interest'] as $a) { echo "<i>$a</i><br />"; } } else { echo 'Nothing selected'; }}------------------------my question is would i need to implode this array to extract the values to use them in this query--------------------$q = "INSERT INTO product_interest (bathsafety, uvSafety, hotWarningLabel, feedingSpoon, bedroomAndNursery) VALUES ('$ bathsafety','$ uvSafety', '$hotWarningLabel', '$feedingSpoon', '$bedroomAndNursery')"or would the query be$q = "INSERT INTO product_interest (bathsafety, uvSafety, hotWarningLabel, feedingSpoon, bedroomAndNursery) VALUES ( '$product_interest)"any help/advice would be nice. :)cheersDarren

Link to comment
Share on other sites

The product_interest array is only going to contain values for what they selected. If they only checked the first 2 boxes, then product_interest is going to be an array of 2 elements with the values "bedroomAndNusery" and "bathSafety", the other values aren't going to be there. You need to look at which values are set and change the appropriate values in your script. I don't know what type of fields are in the database or what information you're trying to save, but this will work if the fields are number fields and you just want a 1 if they checked the box and a 0 otherwise.

$bathSafety = $uvSafety = $hotWarningLabel = $feedingSpoon = $bedroomAndNursery = 0;for ($i = 0; $i < count($product_interest); $i++){  ${$product_interest[$i]} = 1;}$q = "INSERT INTO product_interest (bathsafety, uvSafety, hotWarningLabel, feedingSpoon, bedroomAndNursery) VALUES ('$bathsafety','$uvSafety', '$hotWarningLabel', '$feedingSpoon', '$bedroomAndNursery')"

That code assumes that the possible checkbox values match the variable names.

Link to comment
Share on other sites

Hi people,Thanks for your replies.@CloneTrooper9494I used array because I was using checkboxes, all the examples I had read more or less implied that it would be best using arrays when dealing with checkbox values.@justsomeguyI originally wanted the string value to be inserted into the table field but working with checkboxes, tutorials used numerical values, so i was seeing if i could do it like that really, but i thought i would have further trouble retrieving that data and displaying it as a string. i think im going to use this:-------------------------------- //if(isset($_POST['bathSafety'])) {//$bathSafety = $_POST['bathSafety'];//if($bathSafety =='bathSafety') {//$bathSafety ='checked';//}//need to add security//$q = "INSERT INTO product_interest (bathsafety) VALUES ('$bathSafety')"--------------------------------cheersDarren

Link to comment
Share on other sites

Hi people,just thougt i'd post back the outcome and what i settled for.As i had some problems dealing with the input as an array, i thought i would just treat it as a normal input.I didnt think about security as being an issue but its best to be safe so i used preg_match in my codehere is what i ended up using....(i have only used one product in this example)$bS = FALSE;$trimmed = array_map('trim', $_POST);if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['bathSafety'])) { $bS = mysqli_real_escape_string ($dbc, $trimmed['bathSafety']); } else { echo '<p class="Error">bath safety checkbox</p> '; }if ($bS) { // If everything's OK... //insert into database$q = "INSERT INTO product_interest (bathSafety) VALUES ('$bS')";$r = mysqli_query ($dbc, $q) or 'insert failed something messed up';}cheersDarren

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...