Probably very simple this one, but it's still troubling me.
I have a form, embedded in php, which I need to perform validation on.
An example of one of the fields is:
Server 1:
</td>
<td>
<input name='1_server[1]' value='$defaultserver1' id='server1' required oninvalid="this.setCustomValidity('Please enter server name/IP Address for this connection')">
</td>
</tr>
...as per the above, I have a default value that can be set/inserted when the page loads, but I mainly want to cater for if this isn't set.
If you delete the field, the validation works fine. The problem is, retyping in the field still persist to show the invalid warning.
To Summarise:
If field is empty, and the form submit button is clicked, validation warning shows. You can't, however, fill in the form after this point, as it seems to get 'stuck' in think that the field is 'invalid', even if you complete it.
Can anyone help? I've no doubt I'm being soft...
Edit: Fiddle here https://jsfiddle.net/v03sb4hq/
Resolved this.
setting the oninvalid="setCustomValidity('please fill in')" flag will, on invalid input, set the Validity to a fixed state of 'invalid'.
To counter-act this, I need to state a cleared flag when an input is detected on the same field, i.e. oninput="setCustomValidity('')"
So, my revised code is now...
<input name='1_server[1]' value='$defaultserver1' id='server1' required oninvalid="setCustomValidity('Please enter server name/IP Address for this connection')" oninput="setCustomValidity('')">
Fiddle updated here: https://jsfiddle.net/v03sb4hq/2/
Is $defaultserver1 a php variable? In that case you'd have to echo it within the HTML code, I think.
<input name='1_server[1]' value='<?php echo $defaultserver1;?>' id='server1' required oninvalid="this.setCustomValidity('Please enter server name/IP Address for this connection')">
Related
it might seem weird but i have a strange problem. when i submit a form and check the request from network console of chrome developer tool it shows all the data is submitted but when i get that data in my update file it won't show all the data. the data is submitted as array. like i have below input field in form. total of 330 field we have with the same name and when we add new dynamic field and put data into it and then submit it won't save.
<input type="text" name="txtOptions[]" id="txtOption<?= $i ?>" value="<?= formValue($sOption) ?>" maxlength="100" size="25" class="textbox title" />
when is get on server side and print the data it won't show all the data.
$sOptions = IO::getArray("txtOptions");
print_r("<pre>");
print_r($sOptions);
print_r("</pre>");
exit();
I've tried hard to find out what's going on there is hidden field in my form of MAX_FILE_SIZE. I also try to change the value of this and make it more but nothing happen
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
This only happening on live server not on localhost. any idea. sorry for not to be clear of my question but this is what i have first time in my life.
The IO::getArray are the custom classes functions.
i've found the solution. Actually i have above 1000 input variables in my form which is been generated dynamically and in my php.ini the max_input_vars has been set to 1000 only,that's why i was getting only 1000 input variable values not more than that.
i had no idea that the issue will be the number of input variables. I thought it might be an issue of file size.
I have a website on which the user has to enter quite a number of data using text boxes. I want to lock the textbox after the first use. I mean for the example the user has to enter his name and save it but once saved, the user should not be able to change it again.
So can anybody suggest me what changes has to be made in this code
<td align="left" valign="top">
<input class="tooltip v_empty" title="first name" type="text"
name="<?php echo "LP".$lp_id."_"; ?>firstname[self]"
id="firstname[self]"
value="<?php echo $PLAN->lp[$lp->lp_id]->info['self']->firstname; ?>" />
</td>
You should use a better title. the disabled attribute in html may help you.
http://www.javascriptkit.com/javatutors/deform3.shtml
The <input> HTML elements take a disabled="disabled" argument which renders the user unable to normally modify the contents of the field (it will be greyed out). (Take note, that local HTML source modification can result in changing the data!)
Apart from that, you should take a look at how sessions work. You should store the already entered value in a session, or the database backend, and when the form is getting printed, you should check against what value is already present in the session or database.
<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
This works fine, however when you click on submit, and the checkbox is ticked, it passes BOTH the hidden value and the original checkbox value to the $_POST variable in php, can this be avoided?
I have the hidden value there, so that unticked checkboxes are passed to the $_POST variable as well as the ticked ones.
The better approach is to remove the hidden field, and simply have a check in PHP:
if ($_POST['check_box_1']=='1') { /*Do something for ticked*/ }
else { /*Do something for unticked*/ }
You shouldn't need the hidden field. You should in fact not trust any of the form fields sent in the first place. What this means is that you cannot make code which takes the sent fields and trust them to send the correct data (which I assume you do now).
What you should do is to handle all fields you expect to get. That way if you don't get the checkbox value you can still handle that as if it was unticked. Then you also get the added inherent feature of throwing away form data you don't expect in the first place.
No, it will pass all the form data, whatever it is. The right way to do this is not to set the checkbox via a hidden field but to set the checkbox with whatever its state actually is!
I mean... why are you adding the hidden field to begin with?
Your PHP is receiving two fields named check_box_1, and last time I checked there was no way to guarantee that the params would get read into the REQUEST hash in the exact same order as you sent them, so, there's no way to tell which one will arrive last (that's the one whose value will get set). So... this is not the right approach to whatever problem you're trying to solve here.
Welcome to Stack, btw! If you find answers useful or helpful, make sure to mark them as correct and vote them up.
That's normal.
They must be both type="checkbox" to pass only 1 value.
If you want to get only 1 in any cases you can do:
<input type="checkbox" style="display:none;" name="check_box_1" value="0">
Make sure the first input field is of type Checkbox, or else it won't behave like one.
<input type="checkbox" name="check_box_0" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
Everything is working normal with your code so far.
I'm assuming you are creating the hidden field so that 0 is passed to the server when the checkbox is not checked. The problem is that they both get passed when the check box is checked.
As Death said, the way you should be doing it is with a single checkbox and then checking if the value has been sent to the server or not. That's just how checkboxes work.
If you want to have a default set then you will have to handle all that on the server side based on weather the checkbox has a value.
For example:
$myValue = "";
if(isset($_POST['check_box_1']))
{
$myValue=$_POST['check_box_1'];
}
else
{
$myValue="0";
}
I have a form that I need to have required fields filled out. I know to use the code below to verify if the field is blank:
<?php
if (!empty($_POST['client_name'])) {
echo '<p style="color:red;">'"Client Name is required!"'</p>';
}
?>
My question is, how do I get the error message to display on the form page, saving all the data already entered in the form. Example: I fill out all 15 fields on the form, excluding the required field. When I hit the submit button, if the required field is empty, I want to stay on that form page, without losing any of the info I put into the fields, and I want to display a message next to the required field box, saying "This is a required field.
I am not sure on the code to do that, or where to put it. On the form, or on the script that executes the form?
use client side javascript validation first, then php server side validation.
Why you use !empty you can use empty for best result like
<?php
if (empty($_POST['client_name'])) {
echo '<p style="color:red;">'"Client Name is required!"'</p>';
}
?>
Actually you should be first set HTML5 validation like
<input type="text" name="abc" required="">
You can set custom error message for required field like
<input type="text" name="abc" required="" oninvalid="this.setCustomValidity('Please Select This')">
Then you can use JS or jQuery validation and then user Server side Validation like PHP or ASP or others.
Thanks.
Without knowing the structure of your pages, it's hard to give an exact answer, but here's a general process flow that should help:
Form is submitted to processor
Processor validates inputs
if inputs are good, processor redirects to next page
if inputs are not good, processor should send error text and form data back to the routine that builds/displays the form.
IMHO, the processor should not echo anything. All display should be handled by the script that builds the form.
Without coding it for you, that's the best answer I can give :-)
How do i keep the information a user has entered in a form in a case where s/he has made an error in the form so that s/he does not have to make the entries once again?
I would also like to indicate the errors made by highlighting the texboxes with the errors with a red color
There is no "magical" solution : when the form has been submitted with an error, you have to :
redisplay the form,
filling out the fields with what the used did input (what you received in $_GET or $_POST), putting those data in the right attributes
i.e. value for <input>
or as content for <textarea>
or setting the selected attribute for <select>
and, not forgetting to escape the data, with htmlspecialchars, to prevent HTML injections
For the indication of errors made, a solution is to add a CSS class to the form elements on which you have detected an error.
Adding something like class="error" for <input>s on which there's been an error, and having the .error class properly defined in your CSS file.
Generally the idea is to use:
<input type="text" name="field_1" id="field_1" class="<?= ($errors['field_1'] ? 'error':'') ?>" value="<?= #$_POST['field_1'] ?>" />
This fills the value of the text box with whatever the user inputed and gives you something to style if your script to handle this form deems this value to be invalid.
e.g.
if(empty($_POST['field_1']))
$errors['field_1'] = 'Cannot be blank';