Jump to content

Accept input with dimensions and store in database


vikaspa

Recommended Posts

Dear All

I am accepting following 2 field in a form

one is checkbox

and another is input  

 

<input name="weekday[]" type="checkbox"    value="<?php echo $rows['id'];?>" class='weekday' /> <?php echo $rows['name'] ;?>   
                         
                           <br>Timing
                            
                     <input type="text" name="timing[]"  '    class='timepicker  '  />

 

the form look like this (refer image)

 

after submit i can get checkbox value

 

After submit 

I tried

 

$cntr=count($_POST['weekday']);
 
             for($i=0; $i <     $cntr=count($_POST['weekday']) ; $i++)
            {   
                  
                echo "<br>".$i." Loop Week day =>". $_POST['weekday'][$i]." timing  =>". $_POST['timing'][$i];
            
            //  Addcenterdaystime($staffcompanyid, $newid, $_POST['weekday'][$i],  $_POST['timing'][$i]  ) ; 
            } 

However could not get values of timing

pls help

Opera Snapshot_2022-12-29_130159_dimension input.png

Link to comment
Share on other sites

only checked checkboxes will be set. if no checkboxes are checked, $_POST['weekday'] won't be set and any reference to it will produce a php error. you must test if $_POST['weekday'] isset() before referencing it.

next, to associate each timing[] field to the corresponding weekday[] field, you should use the id as the array index for both fields, and forget about using a value attribute for the checkboxes. after you have determined that $_POST['weekday'] isset(), you can get the ids/keys (see php's array_keys()), then loop over the ids and access the corresponding timing[] field values.

Link to comment
Share on other sites

I really appreciate prompt reply

 

As specified by you I am getting values for $_POST['weekday'][$i]

 code

do {  //reading weekday table and assigning id as index
                 
?>
<tr>

  <td class='text15'  >
    <input name="weekday[<?php $rows['id'];?>]" type="checkbox"    value="<?php echo $rows['id'];?>" class='weekday' /> <?php echo $rows['name'] ;?>   


    <br>Timing

    <input type="text" name="timing[<?php $rows['id'];?>]"     class='timepicker timing'  />

  </td>

</tr>

  <?php } while ($rows=$query->fetch()); ?>

 

 after submit 

 

$cntr=count($_POST['weekday']);
			 
			 echo "weekday count=".count($_POST['weekday']);	
		    
		      echo "timing count=".count($_POST['timing']);
			
 			for($i=8; $i < 	18 ; $i++)
			{   
				  
				echo "<br>".$i." Loop Week day =>". $_POST['weekday'][$i]." timing  =>". $_POST['timing'][$i];
			 
			} 

  output

weekday count=5
8 Loop Week day => timing =>
9 Loop Week day =>9 timing => 
10 Loop Week day =>10 timing =>
11 Loop Week day =>11 timing =>
12 Loop Week day =>12 timing =>
13 Loop Week day =>13 timing =>
14 Loop Week day => timing =>
15 Loop Week day => timing =>
16 Loop Week day => timing =>
17 Loop Week day => timing =>

We get the values for

weekday[9], weekday[10], weekday[11], weekday[12], weekday[13] 

------------------------------------------------------------

but not getting any values for timing[9], timing[10], timing[11], timing[12], timing[13] 

 

pls help

Link to comment
Share on other sites

almost none of that is what i wrote, and you must echo the id in the field array index for it to be output (check the view source of the output in your browser.)

see the following example (uses made up checkbox data instead of sql query data) -

// for debugging, examine the input data
echo '<pre>'; print_r($_POST); echo '</pre>';

// post method form processing
if($_SERVER['REQUEST_METHOD']==='POST')
{
	// test if any checkbox is checked
	if(isset($_POST['weekday']))
	{
		// get and loop over the checked checkbox ids
		foreach(array_keys($_POST['weekday']) as $id)
		{
			// access the timing field value
			echo "Weekday id: $id, Timing: {$_POST['timing'][$id]}<br>";
		}
	}
}

// make up some checkbox data
$days = [];
$days[] = ['id'=>1,'name'=>'Monday'];
$days[] = ['id'=>2,'name'=>'Tuesday'];
$days[] = ['id'=>3,'name'=>'Wednesday'];
$days[] = ['id'=>4,'name'=>'Thursday'];
$days[] = ['id'=>5,'name'=>'Friday'];

?>
<form method='post'>
<button type='submit'>Submit</button><br>
<?php
foreach($days as $row)
{
	$chk = isset($_POST['weekday'][$row['id']]) ? 'checked' : '';
	?>
	<input name="weekday[<?=$row['id']?>]" type="checkbox" <?=$chk?> class='weekday'> <?=$row['name']?>
	<br>Timing<br>
	<input type="text" name="timing[<?=$row['id']?>]" value="<?=$_POST['timing'][$row['id']]??''?>" class='timepicker'><br>
	<?php
}
?>
</form>

 

 

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.