I'm using the form validation library and have something like this in the view
<p>
<label for="NAME">Name <span class="required">*</span></label>
<?php echo form_error('NAME'); ?>
<br /><input id="NAME" type="text" name="NAME" class="" value="<?php echo set_value('NAME'); ?>" />
</p>
I'd like to add a class to the input that is dependent on the form error
so i could have something like this
<input id="NAME" type="text" name="NAME" class="<?php echo $error;?>" value="<?php echo set_value('NAME'); ?>"
I realize that it would have to be a bit more complicated, but I'd like to this this without creating a custom rule callback for every field.
Thanks
~Daniel
If you set all your validation fields in your controller using:
$this->validation->set_fields()
You will have access to any possible error messages, which you could use in the following manner:
<input id="NAME" name="NAME" value="<?php echo $this->validation->NAME; ?>" <?php echo (!empty($this->validation->NAME_error)) ? 'class="error"' : ''; ?> />
http://codeigniter.com/user_guide/libraries/validation.html
As pointed out in the comments, the code above references the old validation library. The new 1.7 way to perform this task would be:
<input id="NAME" name="NAME" value="<?php echo set_value('NAME'); ?>" <?php echo (!empty(form_error('NAME')) ? 'class="error"' : ''; ?> />
I just realized, there is a way to do this with error delimiters
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
I this makes it much easier.
But just out of curiosity, Is there any way of doing this with class in the input?
Related
If the above title looks confusing then here is the description....
I have a template page where I have placed the wordpress default registration form. Now what exactly I want is to add few extra fields on that form.
The wordpress registration will go on as it is. I mean the username and email and password gets stored on the database but along with that new fields or extra details like phone/address/age etc etc gets emailed to a specific email id.
<form method="post" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" class="wp-user-form">
<div class="username">
<label for="user_login"><?php _e('Username'); ?>: </label>
<input type="text" name="user_login" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" id="user_login" tabindex="101" />
</div>
<div class="password">
<label for="user_email"><?php _e('Your Email'); ?>: </label>
<input type="text" name="user_email" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" id="user_email" tabindex="102" />
</div>
<div class="login_fields">
<?php do_action('register_form'); ?>
<input type="submit" name="user-submit" value="<?php _e('Sign up!'); ?>" class="user-submit" tabindex="103" />
<?php $register = $_GET['register']; if($register == true) { echo '<p>Check your email for the password!</p>'; } ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" />
<input type="hidden" name="user-cookie" value="1" />
</div>
</form>
**Note:- The new fields aren't added here.
Is it possible? If yes, then how? Should I add a second form below this form which fires the email? Kindly please suggest an appropriate solution for this.
I have the following line of code:
<input id="b_first_name" type="text" name="b_first_name" class="required"
value="<?= $this->settings['fill_form'] ? "Bob" : "" ?>">
Is there a cleaner way to write this? I don't like writing a ternary where one side doesn't need output, it feels dirty. I am looking to output "Bob" ONLY if the setting "fill_form" is set to true. I wish I could something like this, but neither work:
<input id="b_first_name" type="text" name="b_first_name" class="required"
value="<?= $this->settings['fill_form'] ? "Bob" ?>">
or
<input id="b_first_name" type="text" name="b_first_name" class="required"
value="<?= if ($this->settings['fill_form']) "Bob" ?>">
Missing the echo.
<input id="b_first_name" type="text" name="b_first_name" class="required"
value="<?php if ($this->settings['fill_form']) echo "Bob"; ?>">
But I would put the brackets as well
<input id="b_first_name" type="text" name="b_first_name" class="required"
value="<?php if ($this->settings['fill_form']) { echo "Bob"; } ?>">
This is what my code, problem with all <input> tag, can any one tell me is this the correct way to integrate HTML and PHP
<div style="width:115px; padding-top:10px;float:left;" align="left">Email Address:</div>
<div style="width:300px;float:left;" align="left">
<input type="text" name="email" id="email" value="<?php echo strip_tags($_POST["email"]); ?>" class="vpb_textAreaBoxInputs"></div><br clear="all"><br clear="all">
you need to make sure that $_POST["email"] is set before use for that you can use isset() like
<?php if(isset($_POST["email"])) { echo strip_tags($_POST["email"]); }else{echo 'default' ;} ?>
use ternary operator to display default value for each input instead
<input type="text" name="email" id="email" value="<?php echo isset($_POST['email'])?strip_tags($_POST['email']):''; ?>" class="vpb_textAreaBoxInputs"></div><br clear="all"><br clear="all">
same for firstname
<input type="text" name="firstname" id="firstname" value="<?php echo isset($_POST['firstname'])?strip_tags($_POST['firstname']):''; ?>" >
if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
In my registration page, i used php validation. After the user submit the form if it shows validation errors it also resets all the fields. How can i resolve this problem? Without reset the fields i have to show the php validation errors. I also used in each input value. But it shows
"Notice: Undefined index: domain in D:\xampp\htdocs\deena\domainreg.php on line 82" . Please help me to resolve this problem
<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />
You have to pass all your values to php, and send back to html to feed your fields.
Its not 'resetting your fields' .. Your form is being submitted, hence the page is being reset and fields are therefore loading empty. Place the $_POST[] values in the field values upon page load:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />
Simple. Just add the variables to the input values:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
You should also protect the outputted value, against cross site scripting:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />
In the value field:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
Didn't test it. But i think it should work.
In input tag add the php value as like value="" So that it will echo if the variable is posted or it will show the empty one
how to retain the values of data entry in a placeholder during postbacks
<input type="text" name="name" value="<?PHP $name;?>" placeholder="Enter Name"/>
when php found any error and post it back
The data entry are all gone, user have to re enter all the information again.
What does value="<?PHP $name;?>" do? Is this supposed to print the variable? You would need to do this instead:
<?= $name; ?>
or
<?php echo $name;?>
<?php if empty($name){
$name='Enter Name';
} else {}?>
<input type="text" name="name" <?php echo $name;?> placeholder="Enter Name"/>
Try this:
<input type="text" name="name" value="<?PHP echo empty($_POST['name'])?'Enter Name':$_POST['name'];?>" />
In your PHP use your $_POST / $_GET variables to restore your user data:
<input type="text" name="name" value="<?= $_POST['name'] ?>" placeholder="Enter Name"/>
Where $_POST['name'] contains the data sent via a POST request from the form and 'name' is the name of your input.
Try this;
<input type="text" name="name" value="<PHP if (isset($name)){ echo $name; }?>"placeholder="Enter Name"/>
The isset will handle any undefined variable errors, if $name has not been piror to the submission