Forum Moderators: coopster

Message Too Old, No Replies

Need help when submiting a form

Need help when submiting a form

         

riggerz29

7:38 am on Jun 7, 2010 (gmt 0)

10+ Year Member



Hi all basically ive created a script that generates records dynamically for exercise routines

e.g

exercise 1
squats
reps : dropdown list
sets : dropdown list

exercise 2
Press Ups
reps : dropdown list
sets : dropdown list

when i submit the form i want the value from the dropdowns to be inserted into my mysql but as it is an array i dont the best way to process the array.

here is an example for my code for the form

<div style="margin-bottom:10px;">
<label>
<select name="sets[]" id="sets[]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>

</select>
</label>
<input name="e_id" type="hidden" id="e_id" value="'.$e_id.'" />


here is an expample of my code to preocess the form

while($counter <= count($sets) && $counter <= count($reps))
{
$sets = implode($_POST['sets']);
$reps = implode($_POST['reps']);

$e_id = $_POST['e_id'];
if(isset($_POST['e_id']) !=NULL)
{
$sql2 = "UPDATE tbl_routine_exercise SET re_reps='$reps', re_sets='$sets' WHERE e_ID='$e_id' AND rc_ID='$rc_id'";
$query2 = mysqli_query($myConnection, $sql2) or die (mysqli_error($myConnection));
}
$counter++;
}

if($query2)
{
$content = '<div style="padding:10px; border:dashed 1px #060; color:#063; font-size:12px">Your custom routine is completed & ready to use</div>';
}


any help would be greatly appriciated

Matthew1980

8:58 am on Jun 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there riggerz29,

$sql2 = "UPDATE `tbl_routine_exercise` SET `re_reps` = '".$reps."', `re_sets` = '".$sets."' WHERE `e_ID` = '".$e_id."' AND `rc_ID` = '".$rc_id."' ";
$query2 = mysqli_query($sql2, $myConnection) or die (mysqli_error($myConnection));


On first glance, those two vars a reversed, SQl squery first, then your connection reference. Also assign the $_POST var after you have checked as it is not NULL, and put strip_tags around it & mysql_real_escape_string() as you are using it in a sql query ;) Safer for the DB that way.

I altered your sql formatting too as you are using double quotes IMO it makes the code easier to read and follow ;)

Cheers,
MRb

andrewsmd

8:38 pm on Jun 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not for sure I understand what your problem is, but to submit a dropdown selection use some code like this.
<select name="reps">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

<?php

//this will be the value of the dropdown reps
$reps = $_POST['reps'];


?>

Whatever they select from the dropdown will be stored in the var $reps. Just insert that into the mysql db.

riggerz29

7:11 am on Jun 8, 2010 (gmt 0)

10+ Year Member



hi andrewsmd

Im basically creating the form dynamically based on what exercises are selected so say there are two exercises selected there will be 2 x the below in the form, i just not sure how to process this array.

<label>
<select name="reps[]" id="reps[]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
</label>


any hekp would be appreciated thanks