Can't make my input fields sticky - php

I am trying to making sticky form. To be nice feature for end users, especially if they are requiring them to resubmit a form (for instance, after filling it out incorrectly in the first place).
This is how I do it.
<input type="text" class="form-control" name="name" value="<?php if(isset($_POST['name'])) echo htmlentities($_POST['name']); ?>">
Its working for me. But my problem is if I use required attribute for this INPUT, its not echoing VALUE in the input. But still I can see its value is available when I check it using firebug.
NOTE: It also happen when I using a validation plugin like this.
Can anybody know whats the problem here?
Hope somebody may help me out.
Thank you.

try this one
<?php if(isset($_POST['name'])){ echo htmlentities($_POST['name']); }?>

Related

How to focus an input type text automatically after window.location.href?

I've got this problem since yesterday and I cannot solve it could you please help me?
I want to trigger a focus so when the page is reloaded by the window.location.href function and $_GET is set on the url, it focuses automatically an input type text with the id productCode.
I've tried everything with Jquery, the .focus() method does not work for me.
What can i do?
Thank you in advance guys,
Solution By user toor
<input <?php if (isset($_GET['nit'])) echo 'autofocus'; ?> ...>
I don't sure I understand you clearly, but maybe you may insert "autofocus" parameter into the input tag required.
<input autofocus id="productCode" ... >

PHP echo into form action

Have searched around but not found an answer to this. Might be an indicator that this is a silly question, but:
I'm trying to echo some information from the page URL into a form action so that information is carried to the next page. The form and PHP looks like this:
<form method="link" action="surveymaker.php?title=<?php echo $_GET['title'];?>">
The information that the user enters into the form gets added to the url on the next page, but not the "title" that I'm trying to add by echo. I'm fully prepared to accept that I'm going at this the totally wrong way if that's how it is. Can anyone point me in the right direction?
Thanks
You can use a hidden input field to do this. Just add this to your form:
<input type="hidden" name="title" value="<?php echo htmlspecialchars($_GET['title']);?>">
Note the use of htmlspecialchars to sanitize the value from the URL.
This is not a good idea of archiving what you want ,You should use hidden inputs instead.
example place this anywhere inside your <form>:
<input type="hidden" value="<?= $_GET['title']; ?>" name="title"/>
try this. worked for me..
<?php $title = $_GET['title'];?>
<form method="link" action="surveymaker.php?title=<?php echo $title;?>">

How to make my form sticky when the name field is an array

I'm a student in my last quarter of school working on a project. I'm using JQuery to allow the addition of form fields on the fly, so I have to use arrays for the name field so I can collect the information from potentiall multiple fields that are the same. I'd also like to make said form fields sticky. I've searched Google, searched stackoverflow and asked my veteran developer husband (should have seen the look on his face when I'd stumped him).
I am writing my code like this:
<input type="text" name="trip_date[]" id="trip_date" value="<?php
if(isset($_POST['trip_date']))
{
echo cleanStickyString($_POST['trip_date']);
}
What I'm unsure of, is if $_POST[trip_date] needs to actually be $_POST[trip_date[]], though it seems like that would check to see if the array was set.
Any hint in the right direction would be much appreciated! Honesty, I haven't been able to test it yet because my team mates haven't set up the database yet, but because it stumped my husband, I thought I'd get a jump on it and see if anyone out there had any experience they could share. =/
Thank you for your time. I know you all volunteer your time to help others out, it's appreciated!
Not sure what you mean by "sticky", but it seems you are trying to get PHP to render all inputs added with jQuery (and its values) after the form is submitted. Right?
Try this:
foreach($_POST['trip_date'] as $date) {
echo '<input type="text" name="trip_date[]" value="' . $date . '">';
}
<input type="text" name="trip_date[]" value="<?php echo "blah";?>
if(isset($_POST['trip_date']))
{
if (sizeof($_POST['trip_date']) > 0) {
foreach($_POST['trip_date'] AS $trip_date) {
echo cleanStickyString($trip_date);
}
}
}

Easier way to populate form data and validation data in Code Igniter?

I'm new to code igniter. I'm following the form validation tutorial found at:
http://www.codeignitor.com/user_guide/libraries/validation.html
I find I am typing too much in the view to get the form to re-populate. For example, here's what one of my INPUT fields look like:
<input type="text" name="email" value="<?=$this->validation->email ? $this->validation->email : $array_db_values['email'] ?>" />
Basically, when the form first loads, it should show a value from the database. If the user alters the value and it fails validation, then the form should post the erroneously submitted value.
Does code igniter have a "cleaner" way of rendering this kind of output? Otherwise, I'm going to do something like this in my controller:
$array_db_values = getdbresults();
if($_POST['submit'])
foreach($_POST as $key=>$val)
$array_db_values[$key] = $val;
That way, if postback data exists, it will always override database values.
Then input fields in my view can simply be:
<input type="text" name="email" value="<?=$array_db_values['email'] ?>" />
Is there a better/native CI way to handle this scenario?
I don't think a easier way exists, the best way IMO is to do something like this in your controller:
if (isset($_POST['submit']))
{
$values = $_POST;
}
else
{
$values = getdbresults();
}
No need to loop through the $_POST array, for short:
$values = (isset($_POST['submit'])) ? $_POST : getdbresults();
In your view, you do the same:
<input type="text" name="email" value="<?=$values['email'] ?>" />
Or use the Form helper:
echo form_input('email', $values['email']);
Have you guys looked into the Datamapper Overzealous project?
http://www.overzealous.com/dmz/
It handles validation at the model level. In your form fields, you can set the value to come from the model (either from the database or from your previous post).
<input type='text' name='email' value='<?=$object->data_item ?>' />
I'm pretty new to CodeIgniter as well, but adding this library has made things much easier. That project also comes with a form extension that could really make things easier, but I haven't played much with it yet.
I'mm doing exactly the same as what you are doing. It's a bit cumbersome but not too bad imho.
I felt the same way about laboring over forms, check out macigniter's Form Lib in the Ignited Code forums. It saves a ton of time and is really well-written.

How to save information in inputs inside a form so it doesn't disappear when form is submitted to itself?

I have a picture upload inside a form...
The file is a php file btw...
Problem is whenever this form is filled in, and the user clicks to upload the first picture, the form is submitted to itself and all the fields which the user may have filled in will go blank...
I know of one way to do it, alot of 'isset' in my php code, but is there any simpler or maybe better way I don't know of?
Thanks
You echo back the POST variable on your fields.
<form method="POST">
<input type="text" name="name" value="<?php echo $_POST['name']?>" />
<input type="submit" name="submit" />
</form>
When the form is submitted to self, the same data will be filled.
Well i do not know of anything else. I always use this:
<input type="text" value="<?= isset($value) ? $value : ""; ?>">
I think it is not too much code in the Templates, but it does the Trick.
Alternatively you could use some Frameworks wich abstract everything for you, but i cannot recommend some...

Categories