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>
Related
I have a form and there is a button to append another set of input boxes if you wish to add more information. Everytime it adds a new set of boxes all the input boxes get a unqiue number added on for that set of input boxes.
Example:
If you have three sets of input boxes it would look like this:
name, age, gender, dob
name1, age1, gender1, dob1
name2, age2, gender2, dob2
However, when I send this information over to my php file I extract the information from the array so each one is a variable. So, name would be $name and name1 would be $name1 and so on. But my question is how can I sanitize and validate all the names at once and all the ages at once etc..
The reason I am asking is because I have googled this alot and I can't find an answer on how to do this.
Try to create sets as given in sample below:
For first set:
<input type="text" name="name[]" id="name1" />
<input type="text" name="gender[]" id="gender1" />
<input type="text" name="age[]" id="age1" />
<input type="text" name="dob[]" id="dob1" />
For second set:
<input type="text" name="name[]" id="name2" />
<input type="text" name="gender[]" id="gender2" />
<input type="text" name="age[]" id="age2" />
<input type="text" name="dob[]" id="dob2" />
and set all the further sets accordingly.
Now, to get posted data you can use
<?php
echo "<pre>".print_r($_POST, true)."</pre>";
?>
You may use something like this for each entity:
<input type="text" name="age[]" id="age1" />
Here, id should be in incremental order with JavaScript or jQuery and name should be same which will give you an array for all the attributes in $_POST or $_REQUEST
Print $_REQUEST and you will come to know how exactly you can get all the data.
You are already getting the array for the name, age, etc.
To sanitize use array_map() function in php. It will sanitize the array.
EXAMPLE
$result = array_map("stripslashes", $result);
Here $result is an array
PHP Code to add two numbers , adding happens as soon as i type the second number in the second field with the result populating in the third field.
Can it be done using php or we do use Ajax or other programming languages.
Would be helpful if i could get an answer for this.
Javascript is enough for this purpose
<script type="text/javascript">
function Adder()
{
var a= parseInt(document.frm.A.value);
var b = parseInt(document.frm.B.value);
var result=a+b;
document.frm.Result.value=result;
}
</script>
<form name="frm">
<input id="A" name="A" type="Text" onchange="Adder()" >
<input id="B" name="B" type="Text" onchange="Adder()" >
<input id="Result" name="Result" type="Text" >
</form>
Using jQuery
$('#field2').change(function (){
if(!isNaN($('#field1').val()) && !isNaN($('#field2').val()))
{
$('#field3').val($('#field1').val()+$('#field2').val())
}
});
Where field1,field2, and field3 are the id of fields. It will put the addition in field3 only if the values entered in field 1 and field2 are numeric.
You can do it using javascript.
Script :
function add(){
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
sum = num1+num2;
document.getElementById("sum").value = sum;
}
HTML:
<input type="text" id="num1" name="num1" />
<input type="text" id="num2" name="num2" onchange="add();" />
<input type="text" id="sum" name="sum" />
NOTE : this code is not tested please make relevant changes
I have the following form:
<form action="options.php" method="post">
<input type="text" name="deptid" id="deptid" />
<input type="text" name="deptname" id="deptname" />
<input type="submit" name="submit" id="submit" value="save" />
</form>
EDIT
Is it possible to pass the two values into one associative array BEFORE submission ?
I would like to pass it in this form:
array('deptid'=>'deptname')
I need this because I avoid to modify the script of the destination php file(options.php)
Thanks.
Here is a method using pure HTML that get's you nearly exactly where you want to be, and only uses HTML:
<form action="options.php" method="post">
<input type="text" name="options[deptid]" id="deptid" />
<input type="text" name="options[deptname]" id="deptname" />
<input type="submit" name="submit" id="submit" value="save" />
</form>
Which would give you in PHP:
$post_options = array(
'options' => array(
'deptid '=> '[that input element value]',
'deptname' => '[that input element value]'
)
);
Which you can then (including sanitizing) access such as this:
$post_options = array('options');
if (is_numeric($post_options['deptid'] && $post_options['deptid'] > 0) {
// Do whatever
}
if (is_string($post_options['deptname'] && strlen($post_options['deptname'] > 2)) {
// Do whatever
}
EDIT
Or... You want to reference the deptid in the input name attribute and use it to modify the row for a department name? Which seems to indicate something like this:
<?php
$deptid = 1;
$deptname = 'Department of Silly Walks';
?><input type="hidden" name="options[<?=$deptid?>]" value="<?=$deptname?>">
Which outputs:
<input type="hidden" name="options[1]" value="Department of Silly Walks">
http://codepad.org/DtgoZGe7
The problem with this is that the $deptid value becomes a value that's not actually directly named or referenced. I think this is potentially problematic to implement due to this abstraction of the value from the server to the client and back, so I would recommend what I have at the top instead. It's not much of a difference in practice, but it's more or less self-documenting.
Note, if you wanted to serialize a list of departments, it's a little trickier. You might, for instance, try this:
<input type="text" name="options[][deptid]" id="deptid" />
<input type="text" name="options[][deptname]" id="deptname" />
Which would add an indexed value for every input. However... They were would not be directly associated. So you would get, instead, two zero-indexed arrays for each key.
What I would suggest in this case is to use Javascript to add each new department's input elements, so you can give each a number like:
<input type="text" name="options[0][deptid]" id="deptid" />
<input type="text" name="options[0][deptname]" id="deptname" />
<br/>
<input type="text" name="options[1][deptid]" id="deptid" />
<input type="text" name="options[1][deptname]" id="deptname" />
<br/>
<input type="text" name="options[2][deptid]" id="deptid" />
<input type="text" name="options[2][deptname]" id="deptname" />
<br/>
<input type="text" name="options[3][deptid]" id="deptid" />
<input type="text" name="options[3][deptname]" id="deptname" />
Or do the old-school POSTBACK method and use PHP to count $POST['options'] and "manually" add a new "row" of inputs with the same index. It's a common trap, so you just have to think about it if this is what you're after at some point.
$_POST is already an associative array and I recommend you not to complicate things beyond that because $_POST already holds the data came from your form.
$myassoc = $_POST;
print_r($myassoc);
and the associative array that you will receive is organized and named same in the name attribute of the input elements in your form (including textarea)
Other Insights
As I see your code you want to put the deptname data to deptid as it reaches the PHP server-side code. well the thing you can do with is is just assign it to the key deptid
$_POST['deptid'] = $_POST['deptname'];
$myassoc = $_POST;
print_r($myassoc);
<form method="post">
<input type="text" name="formdata['deptid']" />
<input type="text" name="formdata['deptname']" />
<input type="submit" />
</form>
<?php
if(isset($_POST['formdata']))
{
$deptid = $_POST['formdata']['deptid'];
$deptname = $_POST['formdata']['deptname'];
}
?>
Build a JS object with the appropriate structure, convert it to JSON with JSON.stringify(), POST it, and then do json_decode($_POST['data'],true).
You'll have an exact copy from JS object, to PHP associate array. Drop the second parameter of true to get a PHP object.
$_POST is already an associative array.
You can rebuild an array of the form you need from this by just assigning $_POST to a variable
$myarray = $_POST;
Now $myarray is what you require. Do var_dump($myvar);.
Why would you want to do that?
But, you CAN send "arrays" through forms like this:
<form method="post">
<input type="text" name="textboxes[]" />
<input type="text" name="textboxes[]" />
<input type="submit" />
</form>
<?php
if(isset($_POST['textboxes']))
var_dump($_POST['textboxes']);
?>
$deptid = $_POST['deptid'];
$array = array($$deptid => $_POST['deptname']);
print_r($array);
I need to insert pairs of input values from a form into mysql database. For example here are my input boxes:
<input type="text" name="roomType1" size="30" />
<input type="text" name="roomRate1" size="30" />
<input type="text" name="roomType2" size="30" />
<input type="text" name="roomRate2" size="30" />
<input type="text" name="roomType3" size="30" />
<input type="text" name="roomRate3" size="30" />
etc..
And my sql database is set up as follows:
RoomType
RoomRate
HID
So basically I need to figure out how to pass the two input fields into each field together in the same row. I am not sure if I should do a for loop or how I can get each two and insert it with the same ID. I hope this makes sense. and any help would be GREATLY appreciated!
Those inputs must be part of a form with method="post". Then, on the php side use this:
<?php
$i = 1;
while(isset($_POST['roomType'.$i]))
{
$roomType = $_POST['roomType'$i];
$roomRate = $_POST['roomRate'$i];
// perform your sql statements here
$i++;
}
?>
Since your inputs are paired, it's enough to check if only one of them exists;
<form action="test.php" method="post">
Name: <input type="text" name="fname" />
<input type="hidden" name="fname" value="test" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
How can I read the values of both the fields named fname?
On my action file(test.php) under $_POST, I am getting only hidden field value.
Is there any PHP setting through which I can read both values?
I believe you want to name the fields as:
Name: <input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
to make PHP understand them as an Array.
In case someone wants to do this and doesn't want to change the name of the form elements, or can't, there is still one way it can be done - you can parse the $_SERVER['QUERY_STRING'] or http_get_request_body() value directly.
It would be something like
$vals=explode('&',http_get_request_body());
$mypost=Array();
foreach ($vals as $val) {
list($key,$v)=explode('=',$val,2);
$v=urldecode($v);
$key=urldecode($key);
if ($mypost[$key]) $mypost[$key][]=$v;
else $mypost[$key]=Array($v);
}
This way $mypost ends up containing everything posted as an array of things that had that name (if there was just one thing with a given name, it will be an array with only one element, accessed with $mypost['element_name'][0]).
For doing the same with query strings, replace http_get_request_body() with $_SERVER['QUERY_STRING']
If you want to pass two form inputs with the same name, you need to make them an array. For example:
<input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
You can then access it using the following:
$_POST['fname'][0]
$_POST['fname'][1]
You might want to rethink whether you really need to use the same name though.
Solutions are
1) Try using different name for textbox and hidden value
2) Use an array as mentioned above for field name
3) Its not possible as the values will be overwritten if the names are same