I'm still beginner and still confused what to add on the code.. I have a form and I want to validate it some errors, for example. field empty.. So..
Do I have to add 'else' or mysql_error?
$post = isset($_REQUEST['post']) ? $_REQUEST['post'] : null;
if (empty($post))
{
echo "<p class=\"info\">The post field is empty</p>";
}
Any ideas? Thanks.
I would do:
if (empty($_REQUEST['post'])) {
die("The Post field is empty.");
} else {
// Write to the database
}
a mysql_error(), on the other part, will be useful to debug if anything goes wrong when writing to the database.
Related
I have a form in my website (Wordpress) with several fields. I need to get the data from each field to process it in the logic of my program.
I have tried to use this code and other similar ones that they provide in their documentation but after 8 hours of work I have not succeeded.
Any ideas?
add_action("wpforms_process_complete", 'function_save_custom_form_data');
function function_save_custom_form_data($params) {
foreach($params as $idx=>$item) {
$field_name = $item['name'];
$fiel_value = $item['value'];
// Do whatever you need
echo $field_name;
}
return true;
}
I'm using default delete of Yii. And I just want soft delete a row, it's mean I keep that row and update column deleted from 0 to 1.
That my code in Controller:
public function actionDelete($id)
{
$model = $this->loadModel($id);
if (Posts::model()->countByAttributes(['category_id' => $model->id]) == 0) {
$model->deleted = 1;
$model->update();
}
if (!isset($_GET['ajax'])) {
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
}
Now I just want if Post have any category, it will show popup cannot delete this Post.
Any solutions about this?
Ugh... usualy you would add errors in the validator. Honestly that is the best advice... well the Yii way. You could just check if $_POST is set, mass assign $_POST to $model->attributes and then let model worry about validation. What I would do next is write a custom but simple validation function in the model that would check if there are categories for said model and if yes return true, otherwise false. Then you would just do:
if($model->validate())
{
$model->delete = 1;
if($model->update)
{
// do whatever you wanna do redirect/render...
}
else {
Yii::app()->user->setFlash('error', "Unable to delete post");
// render or redirect... flash is more for redirect but it'll do the trick
}
}
This would solve the backend of it. In the view file you need to display this flash. In the view that you render if it's not deleted add this piece of code:
<?php if(Yii::app()->user->hasFlash('error')):?>
<div class="info">
<?php echo Yii::app()->user->getFlash('error'); ?>
</div>
<?php endif; ?>
I would do it this way... you can do whatever you want to do
I am working on a page that can update the sales agent on a specific order.
I made the list of options and a dropdown list is created.
Heres in the controller:
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is("post")) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
Heres the view:
echo $this->Form->create("Order");
echo $this->Form->input("OrderID");
echo $this->Form->input("UserID");
echo $this->Form->submit("Submit");
echo $this->Form->end();
When I submit the data, it appears that the data is saved, (the flash message is set).
However, when I then preset the fields with data that is already in the database, all the sudden it doesn't even post. (I put a debug after the reques->is("post) condition which doesnt show up after I submit).
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is("post")) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
if (!$this->request->data) {
$this->request->data = $order;
}
The input fields are correctly pre filled, but now the form doesn't even post.
Does anybody know whats wrong?
Thanks!
Update your controller code to:
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is('post') || $this->request->is('put')) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
Now put a debug after the reques->is('post') condition which will show what you need to show.
I have a problem with updating database table in cakephp...
So I have a profile page where the logged in user can see and update his personal information. Here is my solution for it (yes I know it's not the best...)
if($this->request->is('post')) {
$data = $this->request->data['user-profile'];
// $uid = $this->Session->read("WebUser.id");
$uid = 1;
$user_data = $this->WebUser->find('first', array('conditions'=>array('id'=>$uid)));
$updated_fields = '';
foreach($data as $key => $value) {
if($key != 'state'){
if(empty($value)) {
$this->Session->setFlash("Please fill in all the required fields");
return;
}
}
if($user_data['WebUser'][$key] != $value) {
if($key == 'email') {
$mail_check = $this->WebUser->find('first', array('conditions'=>array('email'=>$value, 'id !='=>$uid)));
if($mail_check) {
$this->Session->setFlash("This e-mail is already registered");
return;
}
}
$updated_fields .= "'".$key."'=>'".$value."',";
}
}
if($updated_fields != '') {
$updated_fields .= "'modified'=>'".date("Y-m-d H:i:s")."'";
$this->WebUser->read(null, $uid);
$this->WebUser->set(array('first_name'=>'John','modified'=>'2014-12-30 10:53:00'));
if($this->WebUser->save()) {
$this->printr($this->data);
$this->WebUser->save();
$this->Session->setFlash("Success : Profile data is now modified", array("class"=>"success_msg"));
} else {
$this->Session->setFlash("Error : Data modifying isn't complete. Please try again!");
}
}
return;
}
So this code fetches the user info from the database and looks for those fields which are edited on profile page. Problem is when I want to save the data it give me back false and didn't save it... Does someone have a solution for this?
Try putting
$this->WebUser->validationErrors;
in else part and check whether there are any validation errors or not.
Like this:
if($this->WebUser->save()) {
$this->printr($this->data);
$this->WebUser->save();
$this->Session->setFlash("Success : Profile data is now modified", array("class"=>"success_msg"));
} else {
echo "<pre>";print_r($this->WebUser->validationErrors);die();
$this->Session->setFlash("Error : Data modifying isn't complete. Please try agin!");
}
Did you tried to set id directly?
$this->WebUser->id = $uid;
Also, CakePHP is automatically update modified field in your table after any transactions. So you don't have to set it directly (but you can if you want any other date value).
About validation. I recommend you to read about Data Validation in CakePHP, because that validation is not good, it's can be handled by models.
Also. You can get any validation errors from models using Model::$validationErrors.
debug($this->Recipe->validationErrors);
The problem with this code was that didn't wanted to save the posted data ( debug($this->WebUser->save) has give me back false). At the end I didn't found the solution why this code not worked but the conclusion is that you never need to use more users table than one. So now I have a Users and Groups tables, and different type of users are connected to their groups(Admin, User, ...).
Ok, I have a registration form for my site.
When there are errors up pops a box with the errors. The box is made up of an html div, an h2 header, and php echoing my errors.
If I hit submit it shows the errors like it should, but the header and div show up no matter what.
I dont understand why.
Here is the code block that echoes the errors.
} else if (!empty($errors)){
echo '<div class="msg_module">';
echo '<h2>Registration Errors:</h2>';
echo $General->outputErrors($errors);
echo '</div>';
}
Thanks all!
Edit: I should mention that $errors is an array.
EDIT: My internet is back, here is the outputErrors function:
public function outputErrors($errors = ''){
if(is_array($errors)){
// handle for passed array
foreach ($errors as $error){
if(is_array($error)){
general::outputErrors($error);
} else{
general::$errStr.= ($error != '')?'<li>'.$error.'</li>':'';
}
}
} else if($errors != ''){
// handle for passed string
general::$errStr = $errors;
}
return '<ul id="error_list">'.general::$errStr.'</ul>';
}//outputErrors
Use var_dump($errors); and you will probably see that the variable is not empty.
From your comment, I assume that the variable is of type array
Read the PHP Manual for the empty function and you will see that it returns true for empty arrays (arrays not containing any element)
Now remember that if the variable is of type String, then most strings are considered non-empty.