Welcome Guest, Not a member yet? Register   Sign In
Problems with Form_Dropdown
#1

[eluser]Gary Buckle[/eluser]
I'm brand new to CI.
I seem to be unable to return the right result from the form_dropdown function.
form_input works fine but the form_dropdown doesn't.


My code

Controller addCustomer();

$data['CustomerName'] = array('name' => 'CustomerName', 'id' => 'CustomerName','maxlength' => '100',
'size' => '50',
'style' => 'width:100%');

The $data is passed (as yet) to the view.

Controller customerFormSubmit()
I get the post data
$CustomerName = $this->input->post('CustomerName');



VIEW create a dropdown from possible names.
$contactArray = array();
$this->db->select('contact_id,ContactName');
$query = $this->db->query('SELECT CustomerName FROM customers');
$query = $this->db->get('contacts');

if ($query->num_rows() > 0)
{
foreach ($query->result()as $row)
{
$contactArray[$row->contact_id] = $row->ContactName;
}

}
echo form_dropdown('CustomerContact',$contactArray,'1');

The form_dropdown seems returns the id of the CustomerContact NOT the actual name.
If I use echo $CustomerName; to print out the postdata.


Any ideas ?
#2

[eluser]webthink[/eluser]
A bit confused about this....
$this->db->select(’contact_id,ContactName’);
$query = $this->db->query(’SELECT CustomerName FROM customers’); //<---what's this?
$query = $this->db->get(’contacts’);


You seem to be slipping a query in between your AR methods. $query will get overwritten. This is strange but probably not the problem you're seeing.

After your foreach what does print_r ($contactArray); produce?
#3

[eluser]Gary Buckle[/eluser]
Hi webThink.
Many thanks for the prompt reply. I posted the entry quickly this morning without much thought.
First I pass the form to the view via an array:
&lt;?php
$data['CustomerContact'] = array('name' => 'CustomerContact', 'id' => 'CustomerContact','maxlength' => '100',
pass the data to a view:
$this->load->view('addCustomer', $data);
?&gt;
In the view I make an array from a ContactName field I have in a table called contacts.
Then I try to add the selected ContactName to a field called CustomerContact in a different table.
&lt;?php
$contactArray = array();
$this->db->select('contact_id,ContactName');
$query = $this->db->get('contacts');

if ($query->num_rows() > 0)
{
foreach ($query->result()as $row)
{
$contactArray[$row->contact_id] = $row->ContactName;
}

}
echo form_dropdown('CustomerContact',$contactArray,'1');

echo form_submit('customerFormSubmit', 'Submit'); ?&gt;

The function customerFormSubmit in the controller:

&lt;?php
// get the data from the post array
$CustomerContact = $this->input->post('CustomerContact');
?&gt;

I make a new postdata array to insert into the DB:
&lt;?php
$postData = array('CustomerName' => $CustomerName,'PreviousName'=>$PreviousName,'Building'=> $Building,'Street' => $Street,'Town' => $Town,'County' => $County,'PostCode' => $PostCode,'CustomerContact'=>$CustomerContact,'Notes' => $Notes);

//debugging only
echo 'Post Data';
echo '<br>';
echo $CustomerName;
echo '<br>';
echo 'Customer Contact is ';
echo $CustomerContact;
echo '<br>';

?&gt;

The $CustomerName uses a form_input in the view whereas $CustomerContact is the form_dropdown.

The result is as follows :
Post Data
Test Customer
Customer Contact is 1

So the form_input returns the correct result BUT form_dropdown returns the id ????


Any help is welcome.


Cheers

Gary
#4

[eluser]webthink[/eluser]
It looks to me like you options will be:

Code:
<option value="ID">Name</option>

In which case it is natural for the $_POST array to return the id. Try a 1D array with just the names.
#5

[eluser]Gary Buckle[/eluser]
Hi
The problem was with the $contactArray definition.
Changing to this solves the problem:
$contactArray[$row->ContactName] = $row->ContactName;

Thanks for your help.


Cheers

Gary




Theme © iAndrew 2016 - Forum software by © MyBB