I am new to php (at 60 years old I am converting from years of VBScript and ASP) and having difficulties with form handling. I have searched this and many other forums for a solution and cannot find one. I am trying to delete a record using input from a form;
My form code ($row[0] is the field ID, which auto increments and in this instance has a value of 3):-
<form action="<?=$_SERVER[ 'PHP_SELF' ] ?>" method="post">
<input type="hidden" name="id" value="<?php echo $row[0]?>">
<input type="submit" value="Delete"></form>
The handling page then runs without producing any errors, but the record is not deleted, so I tested the input value on the handling page and the reason appears to be because the value of the variable "id" is not getting through.
I tested the input value on the form page with:
echo $row[0]
and it definitely outputs the integer 3.
The problem manifests on the handling page, and my code for checking that I have received the form input is:
$id = (INT)$_post['id'];
echo 'Form input= ', $_post['id'], '<br>';
echo '$id= ', $id, '<br>';
which displays a blank space for the form input line
and 0 as the value for $id, when both should display the number 3
Can anyone tell me why the value for "id" is not getting sent to the handling page?
I expect the answer is simple but I cannot grasp it.
I hope I have explained the problem satisfactorily but if not, please let me know.
Try this:
$id = (int) $_POST['id'];
echo 'Form input= ' . $_POST['id'] . '<br>';
echo 'id= ' . $id . '<br>';
$_POST is case sensitive in PHP. http://www.php.net/manual/en/reserved.variables.post.php
Strings is concatenated with . not ,
Also '$id= ' would render the value of $id not $id
Related
After a webform is submitted, I redirect it to its "webform result submission page" automatically.
Here all values are shown.
I want to access the values of that submission, to use them in some simple "if then" php statements.
This logic will add some text above that results page. (for example: if the submitted value of formelement_1 == 2 , then add this text "warning, formelement_1 has great value!").
Anybody some input ? Thanks
Try like this way
<?php
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$nid = arg(1); // need to hard-code nid if this is a custom page
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
$first_name = $submission->data[1]['value'][0];
$last_name = $submission->data[2]['value'][0];
$thanks = $first_name . " " . $last_name;
?>
<h2>Thank you <?php print $thanks ?>... Your registration has been sent.</h2>
?>
I hope, it will works.
Thank you for your help ahead of time.
I created an office hours block in Concrete5. Some departments aren't open on certain days, so I'm trying to make a checkbox that if checked displays "Closed" rather than the office hours. Here is an example for Sunday:
<div class="form-group">
<?php echo $form->label("Sundayclosed", t("Sunday (close)")); ?>
<?php echo (isset($btFieldsRequired) && in_array('Sundayclosed', $btFieldsRequired) ? '<small class="required">' . t('Required') . '</small>' : null); ?>
<?php echo $form->text("Sundayclosed", $Sundayclosed > 0 ? date("g:i A", $Sundayclosed) : null, array ('autocomplete' => 'off', )); ?>
</div>
My code works and displays the open and closing time set in the block. However, I'm not sure how to go about adding a checkbox to display "Closed" rather than the time set in the block.
Would anyone mind helping me out? Thank you very much!
If I understand well the piece of code you provided, That's a text box where you input closing time for Sunday and what you would like is a checkbox, on top of the text box, no instead of it, to signal that the shop is closed on sundays and that will show "closed" on your site if closed.
So first, in your block settings you need to add the checkbox:
<?php echo $form->label("SundayTotallyClosed", t("Sunday (totally close)"));
echo $form->checkbox("SundayTotallyClosed", 1, $SundayTotallyClosed); ?>
Then in your blocks view, where you want the "closed" to show:
<?php if ($SundayTotallyClosed) {
echo "Closed";
} else {
// put here the code that normally shows the closing time
}
Be careful when saving your settings, a checkbox send a value to be saved only if it's checked, otherwise nothing is sent. The problem is if you checked and saved it, a value of 1 is saved. If later you uncheck it and save, nothing is sent so nothing is saved so the value remains 1.
The way to deal with that when saving is to make sure you have:
$SundayTotallyClosed = SundayTotallyClosed ? 1 : 0;
And then save it normally
I have a Game model, view and controller, and a newgame action which creates a new game record. That works fine.
I then want to make that form data available to main_game_view.ctp either in the session, or as a passed-in-array (but not visible as a URL parameter). I don't mind if it's done on the session, or passed-in, whichever is the more efficient.
I've tried various different combos of set this, write that, pass the other, but nothing's worked so far. Here's my code as it stands after my latest failure. The GamesController newgame action:
public function newgame() {
if ($this->request->is('post')) {
$this->Game->create();
$this->Session->write('Game',$this->request->data);
if ($this->Game->save($this->request->data)) {
// Put the id of the game we just created into the session, into the id field of the Game array.
$this->Session->write('Game.id',$this->Game->getLastInsertID());
$this->redirect(array('action' => 'main_game_view'));
} else {
$this->Session->setFlash('There was a problem creating the game.');
}
}
}
Writing the game id to the session works perfectly, I can see that in main_game_view when I read() it. But no matter where I put the session write for the request data, I can't find it in the main_game_view.
Similarly, if I try to pass, well, anything into the redirect, I can't find it in either the main_game_view action, or the main_game_view view itself.
Currently my main_game_view action is just an empty function, but I couldn't find anything in there after trying to pass the data via redirect.
Here's the main_game_view.ctp:
<?php debug($this->viewVars); ?>
<p><?php echo 'Game id: ' . $this->Session->read('Game.id') ?></p>
<p><?php echo 'Game name: ' . $this->Session->read('Game.game_name') ?></p>
Game.id is fine, but there's nothing in Game.game_name (which is a valid field name from the model. All my attempts to pass in variables have also failed, the debug line has only ever shown: array().
This seems so simple, having followed the CakePHP blog tutorial and adapting it to create/edit/delete game instances, but obviously something hasn't quite sunk in...
You dont have Game.game_new key in session just by writing: $this->Session->write('Game',$this->request->data); Obviously you have to do something like this in main_game_view:
<?php $game = $this->Session->read('Game'); ?>
<p><?php echo 'Game id: ' . $this->Session->read('Game.id') ?></p>
<p><?php echo 'Game name: ' . $game['Game']['game_name']; ?></p>
I have a user profile update page, which pre-populates the text fields with the users information gathered from $this->request->data. Everything works fine, except the password field is pre-populated with what appears to be the encrypted password from the database, so on save, it fails the validation as the password exceeds the number of characters allowed, and even if it stores, it will then replace the users actual password with the encrypted same password, if that makes sense.
What would be the best work-around for this?
Controller:
public function profile() {
if($this->request->is('post') || $this->request->is('put')) {
if($this->Auth->user("id") == $this->request->data['User']['id']) {
$this->request->data['User']['user_status_id'] = $this->Auth->user("user_status_id");
$this->request->data['User']['user_type_id'] = $this->Auth->user("user_type_id");
if($this->User->save($this->request->data)) {
$this->Session->setFlash('Your profile has been updated','default',array('class'=>'success'));
} else {
$this->Session->setFlash("An error has occured updating your profile.");
}
} else {
$this->Session->setFlash("Unable to save this result as there is an ID mismatch.");
}
} else {
$this->request->data = $this->User->read(null,$this->Auth->user("id"));
}
}
View:
<h1>Profile</h1>
<h3>Update your profile</h3>
<div class="left">
<div class="formContainer">
<?php
echo $this->Form->create('User');
echo $this->Form->input('username',array("id" => "profileUsername"));
echo $this->Form->input('password',array("id" => "profilePassword"));
echo $this->Form->input('company');
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
echo $this->Form->input('telephone');
echo $this->Form->input('fax');
echo $this->Form->input('id',array('type' => 'hidden'));
echo $this->Form->end(__('Update'));
?>
</div>
</div>
The problem you have is, if you have stored your password as you should (with a one way hash), you can't reverse it to show the plain text version so you could simply set the password data to null (leaving the password field empty):
$this->request->data['User']['password'] = NULL;
You then have two options:
Require the users password to update their data (a nice little security measure)
Only update the users password when the user has entered a new password
The 2nd option may break your validation if you have set password validation rules to allowEmpty => false, in this case you could use multiple validation sets which you can do by following either of these (all work in cakephp 2.x) multivalidateable behaviour, multiple validation sets
read this and use this behavior:
http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp
the password is a one-way ticket - due to the technical details of hashs.
basically you never pass the password hash back into the view. always display empty fields (like you should have seen on pretty much every other website in the www).
you then ignore and posted field which is empty. only if the password field is freshly set you actually trigger the validation.
<?php echo $this->Form->input('password', array('label' => false,'type'=>'password','required'=>'false')); ?>
Output
<input name="data[User][password]" type="password" id="UserPassword">
Be careful! use this only on the edit part
$this->Form->input('password',array('value'=>null));
I have a problem with the Form Helper that returned $this->data keeps being empty. In my Forms before there was no problems and I cant figure out what's different here.
For this Form there's not a model containing the data, its just user input for doing a search.
This is my View:
<?php
echo $this->Form->create();
echo $this->Form->input('Postleitzahl');
$options=array('10'=>10,'20'=>20);
echo $this->Form->input('Entfernung',array('type'=> 'select' , 'options'=>array(($options))));
echo $this->Form->end('Suchen');
?>
<?php
echo $this->Form->create(null, array('type' => 'post')); # not sure if that's needed
echo $this->Form->input('Search.Postleitzahl');
$options=array('10'=>10,'20'=>20);
echo $this->Form->input('Search.Entfernung',array('options'=> $options)); # seems shorter and should work
echo $this->Form->end('Suchen');
?>
The above should result into a $this->data array containing something similar to this:
['Search']
['Postleitzahl']: 102929
['Enfernung']: 'foobar'
Just don't double array your array:
'options'=>$options
Not necessarily related to Cake, but the answer to the problem when I had it: if you're including a file upload in your POST, double-check that the file you're uploading isn't larger than the limit specified in your php.ini file.