cant get values from form - php

In consequence from yesterdays post as i was trying to transfer some variables between the controller and the view today I am trying to get data from a form and update the db but am having trouble getting those values.
this is function from the controller which is called from the form of the view
function updateRecords(){
$data2=array('name'=>$this->input->post('first_name'),
'surname'=>$this->input->post('last_name'),
'contact'=>$this->input->post('contact'),
'email'=>$this->input->post('email_address'));
print_r($data2);
}
when I try to print the data2 array I get: Array ( [name] => [surname] => [contact] => [email] => )
this is the code from the view:
<fieldset style="text-align:left">
<legend><h2>Edit Clients Details</h2></legend>
<?php
$this->load->helper('form');
echo form_open('site/updateRecords');
echo form_input('first_name', $records['0']->name);
echo form_input('last_name', $records['0']->surname);
echo form_input('contact', $records['0']->contact);
echo form_input('email_address', $records['0']->email);
echo validation_errors('<p class="error">');
echo anchor('site/updateRecords','Save');
echo form_close();
?>
</fieldset>
<p>
<?php echo anchor('site/add','Add clients');?>
<?php echo anchor('site/members_area','Go Home')?>
<?php echo anchor('login/logout', 'Logout'); ?>

instead of
echo anchor('site/updateRecords','Save');
try use
echo form_submit('mysubmitname', 'Save!');
anchor can't submit form data by default. If you use ajax, create javascript function which serialize form data and post to the server.
i hope this help

There are a couple of ways to have the id be accessible. One is to structure the edit page call so that it has the id in it. Normally I do this by setting up my urls to be something like: http://site.com/client/client_id/edit for the edit page. Alternately, you can stash the record's id in a hidden form field when you call the form up and then pass it back as part of the post.

Related

Array to string convertion Error Cakephp

I am trying to display an icon image and give that image a link with the intern details but not working. I am trying to do like within a cakephp code i am trying to show an image and when a user will click on that image it will show another page with these array('action' => 'detail'), $intern['Intern']['id']) details.Here is my code below. What's wrong with these code
<?php
echo $this->Html->link(($this->Html>image('.img/resource/hover_down_icon.png')),array('action' => 'detail'), $intern['Intern']['id']),array('css' =>'image_down_icon');
?>
Trying to display an image and give it a link,Use this type of method
echo '<img src="hover_down_icon.png" />';
OR
echo "<img src=\"hover_down_icon.png\" /> ";
if you want to send details to another page set the details in a variable and send ,then retrive value at next page by
//Using GET, POST or COOKIE.
$var_value = $_REQUEST['details'];
Try to alter your code with adequate changes
you are using wrong perameter in the action array .you need to use array('action' => 'detail', $intern['Intern']['id']) as one parameter. Try this one
<?php
echo $this->Html->link(($this->Html>image('.img/resource/hover_down_icon.png')),array('action' => 'detail', $intern['Intern']['id']),array('css' =>'image_down_icon'));
?>

Reset dropbox automatically to first value after the users hit submit button PHP

After the user hits the submit button, how do I reset the drop down menu to the "blank" option of the the menu? I am using a MVC set up with php and HTML, and the concrete5 library. THANKS IN ADVANCE! Here is what I have so far:
Controller code:
public function authorize() {
$selectHost = array('' => '');
foreach ($this->host->getHostInfo() as $row) {
if (isset($row['HARDWARE_id'])) {
$selectHost[$row['id']] = $row['host'];
}
}
$this->set('selectHost',$selectHost);
$postCheck=array(array('param' => 'host',
'check' => '^[0-9]{1,50}$',
'error_msg' => 'Invalid Host ID'),
);
$post = scrub($_POST,$postCheck);
if (isset($post['host'])) {
$this->host->authorize($post['host']);
$this->set('test', "<p> The host has successfully been authorized.</p>");
}
else{
$this->set('failed', "<p>Invalid Host ID</p>");
}
}
view code:
<form method="post" enctype="multipart/form-data" action="<?=$this->action('authorize')?>">
<?php
$form = Loader::helper('form');
print $form->label('host', 'Host: ');
print $form->select('host', $selectHost);
?>
<?php
print $form->submit('Submit','Submit');
echo $test;
echo $failed;
?>
</form>
I'm pretty positive that there's no way to override C5's desire to take the POSTed value and use that as the default. Even if, as TWR suggested, you specify a value. (This is typically a good thing, because if the page is POSTed to and there's an error, you don't want to show the value from the database; you want to show what was in the POST).
You can override the form helper pretty easily.
However, I'd suggest that you do a redirect after successful submission (don't redirect after an error -- then the POSTed value will be useful) to a page. You can redirect to another page, or the same one, ideally with a confirmation message. See https://github.com/concrete5/concrete5/blob/master/web/concrete/core/controllers/single_pages/dashboard/blocks/stacks.php#L23 for an example of using redirect.
This is the best practice for your problem but also because, with your current code, if somebody hits refresh, it'll rePOST the data and reauthorize the host.
i think you could extend the form tag with a (javascript) onsubmit action which does the reset.
Since it's a form submit, you just want to change the value of the "drop box"/select in your view. After a submit, you'll have a fresh page load; so, in every case you'll want to display the default value, and not the current value of $selectHost
In your view, change this
print $form->select('host', $selectHost);
to this
print $form->select('host', $selectHost, null);
According to http://www.concrete5.org/documentation/developers/forms/standard-widgets
If the problem is that the Concrete5 form helpers are not behaving as you want, then just don't use them -- instead just use regular HTML form inputs instead.
<form method="post" enctype="multipart/form-data" action="<?=$this->action('authorize')?>">
<label for="host">Host: </label>
<select id="host" name="host">
<?php foreach ($selectHost as $value => $text): ?>
<option value="<?php echo htmlentities($value); ?>"><?php echo htmlentities($text); ?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="Submit" />
<?php
echo $test;
echo $failed;
?>
</form>

Pass select value to PHP: $_POST empty

I'm reading a lot of discussion here to find a solution to my issue (for example: how to pass the id value of a select list in a form using post or get?) but for me is not working.
I have a table with a form inside every row:
<select name="form_type" class="textselect">
<?php foreach($SContentType as $key => $val){ ?>
<option value="<?php echo $key ?>" <?php if($thisObjs[$k]['type']==$key) echo "SELECTED" ?>><?php echo $val ?></option>
<?php } ?>
</select>
When I try to get the $_POST value I have all the variables except the Select.
Here is my $_POST print
Array (
[a] => ACT_ARTICLES
[p] => SUBACT_MODIMAGE
[form_id] => 6454
[obj_id] => 8754
[form_description] => )
As you can see I have all the variables of the form except form_type, the variable of the select.
By definition of the select boxes, your code should be working fine. Is it placed within <form> and </form> tags?
It may have something to do with your multiple table rows. Try doing:
<select name="form_type[]">
Does it show up now?
As you tagged the question as jquery, here is a jquery way to do it. The .. is for any specific class or id you want to give for more specific DOM element.
$(".textselect .. option:selected").text()

Yii Framework: Empty variables after form validation fails

I have a form with two input text fields:
<input id="ModelName_test_0" name="ModelName[test][0]" type="text">
<input id="ModelName_test_1" name="ModelName[test][1]" type="text">
These input fields get generated with the following commands:
<?php echo $form->textField($model,'test[0]'); ?>
<?php echo $form->textField($model,'test[1]'); ?>
Now, when I submit the form I see the values in the POST request. However, when the form submit fails then I can not get the values back into the input fields. Printing the model it shows that there are no values for $test; - is this because $test is an array in the form?
Even after the validation all values are still assigned to the variables:
if($model->validate()) {
echo "<pre>";
print_r($_POST);
return;
}
This returns:
[ModelName] => Array
(
[test] => Array
(
[0] => myFirstInputField
[1] => mySecondInputField
)
)
So the values are in the POST but after the failed validation they are gone and I get empty variables:
[ModelName] => Array
(
[test] =>
)
The variable test is declared safe in the validation rules.
What I want to achieve is:
If the validation fails put the entered values back into the appropriate input text fields.
Any pointers in the right direction would be helpful :)
The problem is, that CHtml::activeTextField expects a model and one of its attributes as parameters. If the attribute is named test then have $form->textField($model,'test');. After your form is submitted, either test doesn't have any values or it is an array (check this to confirm, either echoing it's value or doing a print_r on $model->attributes).
I found this article on the yiiframework.com website which helped me solving this issue: http://www.yiiframework.com/doc/guide/1.1/en/form.table
This is the sample code which you would put it your controller:
public function actionBatchUpdate()
{
// retrieve items to be updated in a batch mode
// assuming each item is of model class 'Item'
$items=$this->getItemsToUpdate();
if(isset($_POST['Item']))
{
$valid=true;
foreach($items as $i=>$item)
{
if(isset($_POST['Item'][$i]))
$item->attributes=$_POST['Item'][$i];
$valid=$item->validate() && $valid;
}
if($valid) // all items are valid
// ...do something here
}
// displays the view to collect tabular input
$this->render('batchUpdate',array('items'=>$items));
}
And this is what the view would look like:
<div class="form">
<?php echo CHtml::beginForm(); ?>
<table>
<tr><th>Name</th><th>Price</th><th>Count</th><th>Description</th></tr>
<?php foreach($items as $i=>$item): ?>
<tr>
<td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
<td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo CHtml::submitButton('Save'); ?>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
Both code snippets are taken from yiiframework.com where you can find more details on how to use 'Tabular Inputs': http://www.yiiframework.com/doc/guide/1.1/en/form.table

cakephp : how to get an array of elements from a web form

In my cakephp form I have following code
<p> <?php echo $form->input('option[]',array('size'=>13)); ?> </p>
<p> <?php echo $form->input('option[]',array('size'=>13)); ?> </p>
<p> <?php echo $form->input('option[]',array('size'=>13)); ?> </p>
<p> <?php echo $form->input('option[]',array('size'=>13)); ?> </p>
I am trying to get values from a set of input text boxes, the number of text boxes can be set by the user, so cant give individual names of each text box, but How can I get values from my controller to insert data to db table
Thank you
You can leave the form as it is (and use suggestions from #Wizzard and #Lee), but the best practice is to use an incrementing variable to construct the list. i.e.:
for($i=0;$i<$option_number;$i++){
echo $form->input("MyModel.{$i}.option");
}
This way your variable after posting the form will look like:
data[MyModel][0][option] = 'the value'
dataMyModel[option] = 'the value'
data[MyModel][2][option] = 'the value'
... and so on...
In the controller you can access the posted data by:
print_r($this->data);
Take a look saveAll() (search for saveAll in your browser and look for suggested data structure)
your input fields are all named the same thing: option[]. This is good. It causes php to automatically turn them into an array when the request is loaded in. So you can get them in your CakePHP controller like this:
$this->params['form']['option'][0]
$this->params['form']['option'][1]
... and so on ...
Pretty sure they're in the array $this->params['form'] in the controller.. or $this->data
In the method of your controller, do a var_dump($this); and you'll see where they show up

Categories