Jump to content

foreach $_POST help


dadamssg87

Recommended Posts

I'm have a form with at least 3 fields(name, price, and date). I have some jquery that produces another date form input every time a certain link is clicked so there can be several date inputs. The first one(not dynamically created) has the form name "date_1". All the jquery produced inputs have the name "in_123435443453". The number after "in_" is the current timestamp. I'm trying to cycle through only the POSTs that start with "in_" and take their value to insert into database. I can't figure out how to grab the date("in_25346435253") POST in the foreach loop though because they're all unique. I'm using the codeigniter framework. That's why the code to insert into the db make look weird.

 

<?php
	foreach($this->input->post() as $key => $value)
	{
	$start = substr($key, 0, 3);

		if($start == "in_")
		{
		    $cal_id = $this->input->post('cal_id');
		    $exc_name = $this->input->post('exc_name');
			$date = $this->input->post('???'); // <-- part i need help with
			$price = $this->input->post('price');		
		    $date = date("Y-m-d 00:00:00",strtotime($date));

		    $data = array(
			               'cal_id' =>$cal_id,
			               'name' => $exc_name,
			               'date' => $date,
			               'price' => $price
			            );
			$this->db->insert('Exceptions', $data);				
		}			
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/237363-foreach-_post-help/
Share on other sites

Change the JavaScript code to create the date input names so they will be interpreted as a sub-array in the POST data.

 

Create ALL the date input fields with the same name followed by brackets. Example

<input type="text" name="dates[]" />

 

Then in your PHP code you can loop through all of the date inputs as follows:

foreach($_POST['dates'] as $dateValue)
{
    //Do something with $dateValue
}

Link to comment
https://forums.phpfreaks.com/topic/237363-foreach-_post-help/#findComment-1219722
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.