when i try to post html from my textarea the variable value is empty and form_validation show error message that the input is required......
i search for solution and i didn't found ....
this is my code
<form accept-charset="utf-8" method="post" action="<?php echo base_url('send_message/validate'); ?>">
<?php
echo validation_errors();
echo form_dropdown('cat_name', $plan_cat);
?>
<div>
<span><label>اسم الراسل باللغه الانجليزية</label></span>
<span><input name="from" type="text" class="textbox" value="<?php echo set_value('from'); ?>" /></span>
</div>
<div>
<span><label>العنوان</label></span>
<span><input name="subject" type="text" class="textbox" value="<?php echo set_value('subject'); ?>" /></span>
</div>
<div>
<span><label>الرساله</label></span>
<?php
/*
$options = array(
'name' => 'message',
'id' => 'elm1'
);
echo form_textarea($options);
*/
?>
<textarea name="message" id="elm1"><?php echo set_value('message'); ?></textarea>
</div>
<div>
<span><input type="submit" value="ارسل" /></span>
</div>
<?php
echo form_close();
?>
and this is my controller code :
$this->load->library('form_validation');
$this->form_validation->set_rules('from', 'الراسل', 'required|min_length[8]');
$this->form_validation->set_rules('subject', 'عنوان الرساله', 'required|min_length[6]');
$this->form_validation->set_rules('message', 'الرساله', 'trim|required|min_length[10]|xss_clean');
if($this->form_validation->run() == false){
$user_id = $this->session->userdata('user_id');
$send = array(
'title' => 'ارسل الرساله',
'plan' => $this->user_plan->get_plan($user_id)
);
$this->load->library('parser');
$this->parser->parse('header', $send);
$this->parser->parse('send_message', $send);
$this->parser->parse('footer', $send);
}else{
$user_id = $this->session->userdata('user_id');
$plan_cat = $this->input->post('cat_name');
$from = $this->input->post('from', true);
$subject = $this->input->post('subject', true);
$message = $this->input->post('message', true);
$message = '<code>'.$message.'</code>';
i hope to find way to solve this :)
First , before going deep in your code , I have to say that : using base_url functionality as action attribute is bad. You have to use site_url instead in this way :
echo site_url('send_message/validate');
The reason stands behind that :
site_url gives you the full URL including the main processing file (index.php) which is important to handle your MVC requests (e.g http://domain.com/codeigniter/index.php/send_message/validate)
base_ur gives you the URL without any consideration for the main file (index.php) (e.g http://domain.com/codeigniter/send_message/validate)
Second (And the important). I see that you are using XSS Filter for the (message) which means you are trying to put illegal content in the textarea. Try to put anything else everything will be ok.
So , make sure that your message value doesn't contain illegal characters.
Related
I installed the Front-End Publishing plugin on my website, and it displays this menu:
FEP menu
I want to remove the 'tags' and 'choose featured image' option from the menu.
Since I want users to upload media only for the post content, I would also like to prevent users from typing words in the content box.
This is the plugin's submission-form.php What code do I have to edit to achive this? (I tried to put all the code in code format but part of the code refused to be put in the code sample box)
<?php
$post = false;
$post_id = -1;
$featured_img_html = '';
if( isset($_GET['fep_id']) && isset($_GET['fep_action']) && $_GET['fep_action'] == 'edit' ){
$post_id = $_GET['fep_id'];
$p = get_post($post_id, 'ARRAY_A');
if($p['post_author'] != $current_user->ID) return 'You don\'t have permission to edit this post';
$category = get_the_category($post_id);
$tags = wp_get_post_tags( $post_id, array( 'fields' => 'names' ) );
$featured_img = get_post_thumbnail_id( $post_id );
$featured_img_html = (!empty($featured_img))?wp_get_attachment_image( $featured_img, array(200,200) ):'';
$post = array(
'title' => $p['post_title'],
'content' => $p['post_content'],
'about_the_author' => get_post_meta($post_id, 'about_the_author', true)
);
if(isset($category[0]) && is_array($category))
$post['category'] = $category[0]->cat_ID;
if(isset($tags) && is_array($tags))
$post['tags'] = implode(', ', $tags);
}
?>
This form needs JavaScript to function properly. Please turn on JavaScript and try again!
<div id="fep-new-post">
<div id="fep-message" class="warning"></div>
<form id="fep-submission-form">
<label for="fep-post-title">Title</label><br/>
<input type="text" name="post_title" id="fep-post-title" value="<?php echo ($post) ? $post['title']:''; ?>"><br/>
<label for="fep-post-content">Content</label><br/>
<?php
$enable_media = (isset($fep_roles['enable_media']) && $fep_roles['enable_media'])?current_user_can($fep_roles['enable_media']):1;
wp_editor( $post['content'], 'fep-post-content', $settings = array('textarea_name'=>'post_content', 'textarea_rows'=> 7, 'media_buttons'=>$enable_media) );
wp_nonce_field('fepnonce_action','fepnonce');
?>
<?php if(!$fep_misc['disable_author_bio']): ?>
<label for="fep-about">Author Bio</label><br/>
<textarea name="about_the_author" id="fep-about" rows="5"><?php echo ($post) ? $post['about_the_author']:''; ?></textarea><br/>
<?php else: ?>
<input type="hidden" name="about_the_author" id="fep-about" value="-1">
<?php endif; ?>
<label for="fep-category">Category</label><br/>
<?php wp_dropdown_categories(array('id'=>'fep-category', 'hide_empty' => 0, 'name' => 'post_category', 'orderby' => 'name', 'selected' => $post['category'], 'hierarchical' => true, 'show_option_none' => __('None'))); ?><br/>
<label for="fep-tags">Tags</label><br/>
<input type="text" name="post_tags" id="fep-tags" value="<?php echo ($post) ? $post['tags']:''; ?>"><br/>
<div id="fep-featured-image">
<div id="fep-featured-image-container"><?php echo $featured_img_html; ?></div>
<a id="fep-featured-image-link" href="#">Choose Featured Image</a>
<input type="hidden" id="fep-featured-image-id" value="<?php echo (!empty($featured_img))?$featured_img:'-1'; ?>"/>
</div>
<input type="hidden" name="post_id" id="fep-post-id" value="<?php echo $post_id ?>">
<button type="button" id="fep-submit-post" class="active-btn">Submit</button><img class="fep-loading-img" src="<?php echo plugins_url( 'static/img/ajax-loading.gif', dirname(__FILE__) ); ?>"/>
</form>
For Question # 1:
Add a class to these elements (I have called the class removal)
<label for="fep-tags" class="removal">Tags</label><br/>
<input type="text" name="post_tags" class="removal" id="fep-tags" value="<?php echo ($post) ? $post['tags']:''; ?>"><br/>
<div class="removal" id="fep-featured-image">
<div id="fep-featured-image-container"><?php echo $featured_img_html; ?></div>
<a id="fep-featured-image-link" href="#">Choose Featured Image</a>
<input type="hidden" id="fep-featured-image-id" value="<?php echo (!empty($featured_img))?$featured_img:'-1'; ?>"/>
</div>
Add custom CSS to your theme:
.removal{
display:none !important;
}
For Question # 2:
You can do two things. You can completely remove the user's ability to type text in by removing the textarea altogether in CSS:
#fep-about{
display:none !important;
}
If for any reason you actually need the text box (not sure why). I would add custom Javascript to detect if the user has entered a key or copy and pasted any text. Considering the ID of the textarea element is fep-about:
var ele = document.getElementById('fep-about')
ele.addEventListener('keyup',function(){
this.value = "";
alert("Only direct media uploads are allowed!");
});
ele.addEventListener('onchange',function(){
this.value = "";
alert("Only direct media uploads are allowed!");
});
Here is the JSFiddle for this: https://jsfiddle.net/a3zsdo88/1/
Compatibility Issues
If there are some sort of issues in CSS classes overriding the above ones, just add the display statements inline with the elements:
<div id="fep-featured-image" style="display:none !important;">
<div id="fep-featured-image-container"><?php echo $featured_img_html; ?> </div>
<a id="fep-featured-image-link" href="#">Choose Featured Image</a>
<input type="hidden" id="fep-featured-image-id" value="<?php echo (!empty($featured_img))?$featured_img:'-1'; ?>"/>
</div>
See how I added the display statement to the parent element. Do that for the elements you do not want shown.
If you don't have a way to customize your Javascript easily, then add a <script></script> block directly in your HTML in your form file.
<script>
var ele = document.getElementById('fep-about')
ele.addEventListener('keyup',function(){
this.value = "";
alert("Only direct media uploads are allowed!");
});
ele.addEventListener('onchange',function(){
this.value = "";
alert("Only direct media uploads are allowed!");
});
</script>
I am trying to validate text area in codeigniter
code in view
Write the best two hoppy you pratise
<?php $user=" "; ?>
<form action= "<?php echo base_url('log_c/save_hoppy/ ' .$user ); ?>" method="post" >
<?php echo form_error('hoppy'); ?>
<textarea name="hoppy" cols="20" rows="2">
<?php echo set_value('hoppy'); ?>
</textarea>
<input type="submit" name="submitbutton" value=" Save ">
</form>
and this code in controller
public function save_hoppy($user)
{
$this->form_validation->set_rules('hoppy', 'Hoppy', 'required|max_length[40]');
echo"after validation".$user."<br>";
$hoppy = $this->input->post('hoppy');
echo"<br>this hoppy".$hoppy;
}
But this validation doesn't work
.the population text area work please any one help me
remove above text area and paste Codeigniter form input
$data = array(
'name' => 'hoppy',
'id' => 'hoppy',
'rows' => '2',
'cols' => '20',
'maxlength' => '40'
);
echo form_textarea($data);
Here is the solution
<textarea name="description">
<?php echo set_value('description'); ?>
</textarea>
I haven't used CodeIgniter in nearly a year and I seem to have forgotten a lot of the fundamentals.
I am trying to retrieve the post variables from a form and pass them into a model which inserts them into mysql.
The controller function my form submits to looks like this:
public function validation() {
$this->load->helper("form");
$this->load->model("contact_form");
$data = array(
"name" => $this->input->post("name"),
... etc. etc. ....
);
if ($this->contact_form->new_form($data)) {
$this->load->view("header");
$this->load->view("submitted");
} else echo "Sorry, there was a problem adding the form to the database.";
}
The form in the view is structured like so:
<? echo form_open("form/validation");?>
<div id="one" style="display: block;">
<h1>A Heading</h1>
<p>Some Text</p>
<p class="bold">Name: <input type="text" name="name" class="single" value="<?php echo set_value('name'); ?>"></p>
<p class="bold">Email: <input type="text" name="email" class="single" value="<?php echo set_value('email'); ?>"></p>
<p class="bold">And then some radio buttons</p>
<p> yes <input type="radio" name="registered" value="yes"> no <input type="radio" name="registered" value="no"></p>
<p class="bold">And a textarea...</p>
<textarea name="description" class="fill" value="<?php echo set_value('description'); ?>"></textarea>
next
</div>
<div id="two" style="display:none;">
<h1>Another Heading...</h1>
<p class="bold">And some more textareas</p>
<textarea name="audience" class="fill"></textarea>
... There are four divs in total with further textarea fields ...
<p class="bold"><input type="submit" name="submit" value="submit" class="center"></p>
back
</div>
<? echo form_close();?>
And finally my model is very simple:
class contact_form extends CI_Model {
public function new_form($data) {
$query = $this->db->insert("contact", $data);
if ($query) {
return true;
} else return false;
}
}
The form processes without any errors, but the data just appears as 0's in MySQL. If at any point I attempt to output the value of $_POST it returns BOOL (false), or with $this->input->post('something'); it returns NULL.
You will notice that no actual validation takes place. Initially I was using $this->form_validation->run() and getting the same results. I thought perhaps I was having trouble with the validation so I stripped it out and now I'm fairly certain my problem is that I'm not passing the $_POST variables correctly.
Can anyone explain why I am failing so hard?
I have now resolved this problem.
For some reason <? echo form_open("form/validation");?> was implementing GET and not POST. Replacing that line with <form method="post" accept-charset="utf-8" action="form/validation"/> resolved the issue.
According to the CodeIgniter documentation, by default, form_open should use POST - I have no idea why in my case it decided to use GET.
I've checked and re-checked my code, referencing the CI docs and other posts throughout the web, but I am unsuccessful at implementing the set_value() method for re-populating form fields after failed validation.
Perhaps I am missing something very fundamental to the CI framework (I'm rather new to it), but your insight would be much appreciated.
I have the following in my controller method:
public function form_step2(){
//Form Setup
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
$data['title'] = $this->base_title;
$data['base_url'] = base_url();
//Validation Settings - must be set per step
$this->form_validation->set_rules('request_type', 'Request Type', 'required');
*...more of the same set_rules()*
if ($this->form_validation->run() === FALSE) {
### Validation failed or New Form
// Get form element data
$data['request_types'] = $this->my_model->get_form_data('request_type');
*...more of the same get_form_data() calls for loading form elements*
//Generate Page from View Templates
$this->load->view('templates/header', $data);
$this->load->view('templates/form_step2', $data);
$this->load->view('templates/footer');
} else {
### Save to database
$this->my_model->set_data($data);
redirect('my_model/success','refresh');
}
}
And in my view, a snippet of the code that is not re-populating:
<?php echo form_open('my_model/form_step2', array('class'=>'form-inline')); ?>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="<?php echo set_value('fname'); ?>" />
<input type="submit" name="submit" value="Submit" />
I can't figure this one out, so thanks in advance for your help.
if you want to use set_data() you need to also use set_rules for that POST/GET field.
Since you've commented out all your set_rules I can not confirm that this is the issue but most likely it is.
please check if you have this line in your code
$this->form_validation->set_rules('fname', 'First name', 'trim|required');
So if you want to re-populate field with name="fname" you need to have set_rules() // as line above for it otherwise it won't process therefore set_value('fname') is empty.
you surely have found a solution but, for people like me which were spending too many time for this trouble.
I found a solution:
so instead to code that
<input type="text" id="fname" name="fname" value="<?php echo set_value('fname'); ?>" />
Try this, it run very well:
<?php $data = array('id' =>fnam, 'name'=> 'fname','value'=> set_value('fname'), 'size =>'50');
echo form_input($data).'<br />'; ?>
Try this way. It will get both validation errors and set value
In View
<?php echo flash_message();
if($this->session->userdata('postinput') !=""){
$value = $this->session->userdata('postinput');
$this->session->unset_userdata('postinput');
}else
$value = "";
?>
<form action="<?php echo site_url('carlisting/carlist');?>" method="post" id="your_reg_form">
<div class="reg-search">
<input placeholder="YOUR REG" name="input" type="text" value="<?php echo $value; ?>">
</div>
In Controller
$this->form_validation->set_rules('input', 'Registration', 'required|min_length[2]|max_length[7]');
if ($this->form_validation->run() == false){
$this->session->set_flashdata( 'message', array('content' => validation_errors(), 'type' => 'error_message_small' ));
$this->session->set_userdata('postinput',$this->input->post('input'));
redirect('home');
}
Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
The above form will show "0" when loaded for the first time.
I am generating form and handling the submit event in the same file.
If user has not entered the title, I want to display the form again and include an error message (e.g. "You forgot the title.").
That means that I have to duplicate code twice - once to diplay empty form and second to display form with body and ask user to enter title:
<?php if(strlen(strip_tags($_POST['posttitle'])) == 0):
// Display the form with question body that user has entered so far and ask user to enter title.
?>
<label for="title"><b>Title:</label><br/>
<input type="text" name="posttitle" id="posttitle" />
<?php endif;?>
<?php elseif ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action']) && $_POST['action'] == 'post') : ?>
<!-- Everything ok - insert post to DB -->
<?php else :
// just display form here (again ouch!)
<label for="title"><b>Title:</label><br/>
<input type="text" name="posttitle" id="posttitle" />
?>
I would do it like this:
If REQUEST_METHOD is POST I will validate the input and collect messages in an array ($errors in my code).
Then I would just print the form and if there was an error the code will print it.
<?php
$errors = array();
function print_value_for($attr) {
if (isset($_POST[$attr]))
echo $_POST[$attr];
}
function print_error_for($attr) {
global $errors;
if (isset($errors[$attr]))
echo $errors[$attr];
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// do validation here and add messages to $errors
// like $errors['posttitle'] = "The title you entered is bad bad bad";
if (empty($errors)) {
// update database and redirect user
}
}
?>
<!-- display the form and print errors if needed -->
<form>
<?php print_error_for('posttitle'); ?>
<input name="posttitle" type="text" value="<?php print_value_for('posttitle') ?>">
<?php print_error_for('postauthor'); ?>
<input name="postauthor" type="text" value="<?php print_value_for('posttitle') ?>">
<?php print_error_for('postbody'); ?>
<textarea name="postbody">
<?php print_value_for('posttitle') ?>
</textarea>
<input type="submit">
</form>
PS. Consider using MVC to separate code and templates.
Here is a quick way to do that.
<form>
<input type="text" name="title" value="<?php echo $_REQUEST['title']; ?>"/>
<input type="text" name="field_a" value="<?php echo $_REQUEST['field_a']; ?>"/>
....
</form>
But I can also advise you to display a var called $title which is the result of a check on $_REQUEST['title].
You could use an output buffer to grab the form and then assign it to a variable like so:
<?php
ob_start();
include('path/to/your/form');
$form = ob_get_flush();
// then later you can just go
print $form;
?>
Hope this helps
When you display the form, use the possibly empty $_POST values as default field values for both the title and question body. If either is empty, the form will display the second time with the other already filled in:
<?php
$message = "";
if (empty($_POST['title'])) $message .= " Please enter a title.";
if (empty($_POST['body'])) $message .= " Please enter a body.";
?>
<form action='me.php'>
<input name='title' type='text' value='<?php if (!empty($_POST['title'])) echo htmlentities($_POST['title'], ENT_QUOTES); ?>' />
<textarea name='body'><?php if (!empty($_POST['body'])) echo $_POST['body']; ?></textarea>
</form>
Read this MVC
Your can write form in view, handler in controller, and business logic in model