I'm using CakePHP 3.4
I have a form with two submit buttons like
<?= $this->Form->create($post) ?>
<?= $this->Form->control('title') ?>
<button name="submit_type" value="draft" type="submit">Draft</button>
<button name="submit_type" value="publish" type="submit">Publish</button>
<?= $this->Form->end() ?>
According to w3schools button value is also sent to server.
But when I debug
debug($this->request->getData('submit_type'));
it gives NULL. Also debugging getData(), it gives only title field.
How to get value of submit button?
I had the same problem. Try to use "input type" instead of "button", like
<input name="submit_type" value="Draft" type="submit" />
<input name="submit_type" value="publish" type="submit"/>
Hope, this will help you. :)
Related
Sorry I am new to WordPress and still having difficulty to retrieve my data from my custom database. So far this is my code for submitting data to my wp_datareg table.
<?php
/*
Template Name: Data-Register
*/
get_header();
?>
<?php
If($_POST['Submit']) {
global $wpdb;
$Data1=$_POST['Data1'];
$Data2=$_POST['Data2'];
if($wpdb->insert(
'wp_datareg',
array(
'Data1' => $Data1,
'Data2' => $Data2
)
) == false) wp_die('Database Insertion Failed');
else echo '<h2>Database Insertion Succesful</h2><p />';
?>
<?php
}
else // else we didn't submit the form so display the form
{
?>
<
<h4>Data Registration Form</h4>
<form action="" method="post" id="addcourse">
<p><label>Put Data1:<input type="text" name="Data1" size="30" /></label></p>
<p><label>Put Data2: <input type="text" name="Data2" size="30" /></label></p>
</div>
<input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
</form>
What I want to add is put another text form and search button where user can search a data of Data1 and edit its value on the form, (please see the image)
Sorry for being such a noob.
PLEASE SEE THIS IMAGE
Thank you.
br
We can fetch data from wp_datareg table using this code, however putting back the data to the form is my problem.
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_datareg" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->Data1;?></td>
</tr>
I am learning word press. I have 2 questions
I have created a page and assigned my template file for it.
1. Question
How to get my form input values in same page or in another page template?
<form id="formId" action="" method="post">
<input style="width:88%" type="text" name="name">
<input type="submit" name="subs" value="Submit" />
</form>
In same file I put:
<?php
if( isset($_POST['subs']) ) {
echo $_POST['name'];
}
?>
I also tried $_SERVER['PHP_SELF']; in action but it's not displayed.
How could I store it in my own table?
When click submit, it refreshes and displays "No Results Found".
2. Question
How to show the submitted values in wp-admin?
Here's the word press code flow I can't solve.
Change Form action to the same page:
<form action="<?php the_permalink(); ?>
try to use
action="<?php echo get_permalink(); ?>"
instead of
action="<?php echo $_SERVER['PHP_SELF']; ?>"
in your form
I can get Cake to output a submit button using the following PHP:
<?php echo $this->Form->end(__('Submit')); ?>
which outputs this HTML:
<input type="submit" value="Submit">
but I want to use a specific input class to get the following:
<input type="submit" value="Submit" class="some class">
is this possible?
Thank you :).
Yes it's possible :
<?php
echo $this->Form->submit(__('Submit',true), array('class'=>'some class'));
echo $this->Form->end();
?>
Which is documented here.
This should do the trick:
<?php echo $this->Form->end(array('label' => __('Submit', true), 'class' => 'some class')); ?>
Is there any way of changing the validation error div structure? I want to insert an image just before the div.
Each error will be displayed inline with the input field, so I want to insert a left arrow image before the validation div.
Currently I am getting this:
<div class="input password required error">
<label for="StudentPassword1">Password</label>
<input type="password" name="data[Student][password1]" value="" id="StudentPassword1" class="form-error">
<div class="error-message">notempty</div>
</div>
I'd like:
<div class="input password required error">
<label for="StudentPassword1">Password</label>
<input type="password" name="data[Student][password1]" value="" id="StudentPassword1" class="form-error">
<img src='...' />
<div class="error-message">notempty</div>
</div>
How would you accomplish this? I'm guessing that I have to modify the core? Thanks
I'm guessing that I have to modify the core?
No. If your app requires changing Cake's core you are likely doing something wrong. Cake has a lot going on and it takes care of a lot of things for you. This happens to be one of the things you can customize to your liking.
When you create your Forms with the FormHelper you can specify default options for input() and one of those options includes the HTML structure and class used to wrap error messages.
I would just modify the CSS of the error-message class to include the image.
.error-message {
background:url( path/to/img.png ) no-repeat top left;
padding-left:40px; /* or whatever you need for the image to fit */
}
<?php echo $this->Form->create('User', array('class'=>'form-horizontal',
'inputDefaults' => array('error' => array(
'attributes' => array(
'wrap' => 'label', 'class' => 'text-error'
)
) )
)); ?>
This will make you customize the error output.
Let's say I have field that looks like this in the view:
<li class="bigfield">
<?php echo $form->input('phone', array(
'placeholder' => 'Phone',
'label' => false,
'between' => '<br />'
)); ?>
</li>
If I have a validation rule on this field and validation fails, I see the following HTML:
<li class="bigfield">
<div class="input text required error">
<br>
<input name="data[Appointment][email]" type="text" placeholder="Email"
maxlength="45" value="" id="AppointmentEmail" class="form-error">
<div class="error-message">Please enter a valid email address</div>
</div>
</li>
I'm like to do something like move the error message div to an entire different part of the page rather then have it inside with the same <li> as the field itself. What would be the most straight forward way of doing this?
Just updating an old post.
The validations errors are automatically passed on to view (as pointed out by #Angel S. Moreno)
$this->validationErrors
In you controller:
$this->set('validationErrorsArray', $this->ModelName->invalidFields());
You will have $validationErrorsArray in your views.
UPDATE (Sept. 2014):
From the view
From CakePHP 2.3 you can access validation errors array from the view:
$this->validationErrors;
From the controller
If you tried to save data in the controller you can access validation errors this way:
$this->ModelName->validationErrors;
If you want to validate data before saving do it this way:
$this->ModelName->set($this->request->data);
if ($this->ModelName->validates()) {
$this->ModelName->save();
} else {
$errors = $this->ModelName->validationErrors;
// handle errors
}
Validating Data from the Controller
From controller you can use:
$this->Modelname->validationErrors['TheFieldYouWantToDisplay'] = 'This is not correct'
In your case it would be like this in your controller:
$this->Appointment->validationErrors['email'] = 'Error message'
This code is just to make a custom error message on the fly. But you can also define $validate in the model and do it like how brancer has described it.