HTML Number Field Checking - php

In HTML number field I want that a user enters numbers between 10 to 20 and then 40 to 50... How to implement this inside an HTML text field or a number field.

As in HTML 5 you can do this
<form action="">
<input type="number" min="10" max="20"/>
<input type="submit"/>
</form>
If you are getting value from database/ want to integrate with php values, you can do something like following.
<input type="number" min="<?php echo $minVal ?>" max="<?php echo $maxVal ?>"/>
REMEMBER: when you are doing validation you has to do the validation for front end end back end. And min and max attribute are not only limited to number type, you can use it for date type also refer this

if you dont want to use javascript or jquery you can use min and max attributes
<input type="number" min="10" max="20">
<input type="number" min="40" max="50">

I don't suppose it is possible using html alone. You will have to use JavaScript validation because the condition here is complex with regard to html.

You can try with patter in html5 :
<input type="text" name="range" pattern="^(?:\d[10-20]|\d[40-50])$" >
Check it simple and short using javascript :
function allownums(a)
{
value1 = a.value;
if( (10<value1==value1<20) || (40<value1==value1<50))
alert("vaild")
else
alert("invaild")
}
<input type='text' id='numonly' onkeyup='allownums(this)'>
if you want to using html only you can use two number field:
<input type="number" min="10" max="20">
<input type="number" min="40" max="50">

Related

Is POST method used to send user defined inputs only

Well, I have 2 php files. The first one user form.php has an html form to collect user inputs and send those data to other php file mysql_insert.php via POST method.
The problem is that along with 6 user inputs, I also have to transfer one more variable count(as discussed in code below) which is not input from user.
I tired following approach:
user form.php--->
<?php
$count=file_get_contents("cnt.txt","r");
$count=$count+1;
echo"Welcome!<br/>You are student number $count.<br/>";
?>
<html>
<p>Fill in the following information to save your marksheet to the database:</p><br/>
<form action="mysql_insert.php" method="POST">
Name:<input type="text" name="name" value=""/><br/><br/>
Marks(out of 100)<br/>
Subject 1:<input type="text" name="sub1" value=""/><br/>
Subject 2:<input type="text" name="sub2" value=""/><br/>
Subject 3:<input type="text" name="sub3" value=""/><br/>
Subject 4:<input type="text" name="sub4" value=""/><br/>
Subject 5:<input type="text" name="sub5" value=""/><br/><br/>
<p name="count" value="$count"></p>
<input type="submit" value="Submit"/>
</form>
</html>
I simply named an empty P element as count and set its value as $count to send it along with input variables(correct me if there is something wrong here, I am very novice to php).
And on the receiving end I used following code:
mysql_insert.php--->
<?php
require("connect.php");
$name=$_POST['name'];
$s1=$_POST['sub1'];
$s2=$_POST['sub2'];
$s3=$_POST['sub3'];
$s4=$_POST['sub4'];
$s5=$_POST['sub5'];
$count=$_POST['count'];
.
.
.
?>
Now on the line $count=$_POST['count']; the browser is throwing the error:
Notice: Undefined index: count in C:\xampp\htdocs\Vikas-117-PHP\level 3\mysql_insert.php on line 10
It seems the count is not being posted to this file.
Please guide me where I am wrong.
P.S.: I can of course use the file_get_contents() in the mysql_insert.php and get the count value directly in this file, but I am considering that way as my last option.
So please help if the non user-input variable can be posted via forms???
Thanks a million!!!
you have to put your $count variable in a hidden field in your form.. The data which are put in form inputs only get posted in form submit..
<input type="hidden" name="count" value="<?php echo $count ?>">
<p name="count" value="$count"></p>
you can not use directly any html tag for post data only use html form fields for posting data.
for use html tag values post you need to use js/ajax/php
<p name="count"><?php echo $count;?></p>
or better you use
<textarea name="count"><?php echo $count;?></textarea>
or for data not showing use hidden field
and get by name this p value or use class or id for get and post data using ajax
<p name="count" value="$count"></p>
This is not an input element so you cant post directly. You can however use jquery to get the value and POST using js.
You need to have the value inside input type text or hidden if you want to do the way you are doing now.
Change this line : <p name="count" value="$count"></p>
to the : <input type="hidden" name="count" value="<?=$count?>" />
You can use hidden elements like the following
use
<input type="hidden" value="<?php echo $count; ?>" name="count"/>
instead of
<p name="count" value="$count"></p>
Instead of
<p name="count" value="$count"></p>
Use
<input type="hidden" name="count" value="<?php echo $count;?>">
this line will not work : <p name="count" value="$count"></p>
you can use <input type="hidden" name="count" value="<?php echo $count; ?>" />

handle html input elements with numeric id javascript

i have html form
<form name="updatefrm" id="updatefrm" action="" method="post">
<input type="hidden" name="98" id="98" value="" />
<input type="hidden" name="99" id="99" value="" />
<input type="hidden" name="100" id="100" value="" />
<input type="hidden" name="101" id="101" value="" />
<input type="hidden" name="102" id="102" value="" />
<input type="hidden" name="updateqty" id="updateqty" value="1" />
</form>
now i want to assign value to this elements
i m using following javascript code for element with id 98
document.updatefrm."98".value = elements[0].value;
but its giving me error in console.
can any one help me with this ?
You should use document.formname to access forms as not all browsers support this(actually I'm not sure if any does). use document.forms.formname instead. Also to access a numeric property of an object use bracket notation instead of dot notation.
document.forms['updatefrm']['98'].value = elements[0].value;
Why can't I have a numeric value as the ID of an element?
// Change your numeric id.
var myValue=document.getElementById('your_changed_id').value;
alert(myValue)
Go for simplicity..Instead of writing this complex code.why not try a simple code which does the same thing
document.getElementById('your_changed_id').value
You can also use getElementById to set the value. Beside this also check Javascript Naming Convention
document.getElementById('98').value = "YOUR VALUE";
You should not give numbers as ID include any kind of character..
Try like this:
document.forms["updatefrm"]["s98"].value = elements[0].value;
Fiddle

Access form input in the same form

I am taking input from user in a form. Can I get that value(user input) to calculate other fields in the same form.
<input size="12" id="inputField" name="inputField" autofocus="" type="date" onblur="return dateValidate(this)"/>
Can I collect this form input later in the form and use it to calculate other fields. I was trying to using to use $_POST to retrieve the value but I am not sure if thuis is the right thing.
You cannot do it in PHP before submitting the form. You can easily do so in JavaScript. However you could simply add some AJAX code to send the value to your PHP script when user enters something in the box, and parse the response accordingly. PHP works on server side, and does not interact with user without any server side request
You can certainly do it in 2 ways...
1st way
<?php
$first_digit = '';
$second_digit = '';
$third_digit = '';
if(isset($_POST['calculate'])) {
$first_digit = $_POST['first_digit'];
$second_digit = $_POST['second_digit'];
$third_digit = $first_digit + $second_digit;
}
?>
<form method="POST">
<input type="text" name="first_digit" value="<?php if(isset($first_digit)) echo $first_digit; ?>" />
<input type="text" name="second_digit" value="<?php if(isset($second_digit)) echo $second_digit; ?>" />
<input type="text" name="third_digit" readonly value="<?php if(isset($third_digit)) echo $third_digit; ?>" />
<input type="submit" name="calculate" value="Calculate" />
</form>
2nd Way
Total the variables in the code and instead of showing the result output in a text box you can instead calculate and echo out the result, or you can store that in the database.
Note: Be sure you keep the third input[type=text] as readonly as it is showing you the calculated value so probably you don't want your users to change
Yes you can get input from user to calculate other fields in the same form, you should use scripts like OnChange() or onClick() to have this.

Check type of an input field with php

I would like to check the type of an input field text is. Cause i only want to make thos fields read only. I know i can do this in javascript, but is there any way that this is possible in php?
best regrads.
same as javascript use this
<input name="address" type="text" readonly="readonly">
this make field read only you can't change it.
<input name="address" type="text" value="<?php echo "your value"; ?>" readonly="readonly">
try this code
To disable an input field add disabled attribute:
disabled="disabled"
This has to be done when generating the html or with javascript after generation.
This is a clean way of doing it and the user sees directly that it is just readable but not editable.
When you submit a form, in php you take the name attribute to get the posted value. Say you have
<input name="address" type="text">
In php:
if($_POST['address']){
//don't process address cause you know this is of type text
}
This is how you can do it in php,if $field is false then set input disabled
<input type="text" name="" <?php echo ($field == false)? 'disabled="disabled"' : '';?> value="" />

Combining form textfield values using php implode

I have 3 text fields and I want to pass the values after combining them using a hyphen.
<input type="text" name="val[]" />
<input type="text" name="val[]" />
<input type="text" name="val[]" />
Preferably help me with php implode option.
How do i retrieve it after submit ?
Thanks.
After sending the form, your values will be in $_POST['val'] or $_GET['val'] as an array, depending on the method of your form.
You can combine them simply by:
$hyphenated = implode("-", $_POST['val']); // or $_GET['val']
thanks. how do i change focus to next field once a field has max values:
See if this works:
<input type="text" name="val[]" onkeyup='checkVals("field1", "field2");' id='field1'>
<input type="text" name="val[]" onkeyup='checkVals("field2", "field3");' id='field2'>
<input type="text" name="val[]" id='field3'>
<script>
function checkVals(this_field, next_field){
var fieldval = document.getElementById(this_field).value;
var fieldlen = fieldval.length;
if(fieldlen > 10){ // you can change 10 to something else
document.getElementById(next_field).focus();
document.getElementById(next_field).select();
}
}
</script>

Categories