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);
Related
Hi friends am trying to pass the input type as array.
Here is my code..
<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
echo sizeof($videoid);
?>
}
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>
But am unable to read them for example I want to pass hello,how,are,you,why
but am unable to read them after when I pass them
Here's how I would do it. You only need one texbox. No need for multiple textbox. Try this:
<?php
if(isset($_POST['submit_tags'])){
$tagsList=$_POST['tags_list'];
$videoids = explode(",", $tagsList);
echo sizeof($videoids);
}
?>
Also you can remove the [] from your textbox name name="tags_list[]" to name="tags_list"
1) To send a input value as array to PHP, you need to set one value per input
<input type="text" name="tags_list[]" value="hello">
<input type="text" name="tags_list[]" value="world">
Now the PHP can understand $_POST['tag_list'] as array.
2) You can change your approach and split the string.
<input type="text" name="tags_list" value="hello, world">
And transform the $_POST['tag_list'] string into an array. Example:
$tag_list = explode(', ', $_POST['tag_list']);
Or you can use preg_split function to add more intelligence to your string transformation.
Regards,
To read(convert string) from array you can use implode
<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
echo implode(",",$videoid);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>
Sravya try to understand the concept first, name as an array in a html element is used when you want to make more than one html element as same type so that you can get its value by iterating the array like:
<input type="text" name="tags_list[]"> -> its value is one
<input type="text" name="tags_list[]"> -> its value is two
<input type="text" name="tags_list[]"> -> its value is three
You can print its values like:
print_r($tags_list);
Otherwise use single html element.
try this.
$variableAry=explode(",",$variable); //you have array now
foreach($variableAry as $var)
{
echo $var. "<br/>";
}
<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
$tag_list = explode(', ', $videoid);
print_r($tag_list);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>
What's the ideal way to handle an input form with multiple values, without going through each $POST_['inputname'] individually?
For example, let's say I have an invite form like this:
<form id="input-form" action="" method="post" >
<input id="email_1" name="email_1" />
<input id="role_1" name="role_1" />
<input id="email_2" name="email_2" />
<input id="role_2" name="role_2" />
<input id="email_3" name="email_3" />
<input id="role_3" name="role_3" />
</form>
Ideally, I'd like to create two arrays with the data, one for the email addresses and another for the roles
The resulting arrays would look like this:
$emailArray = array(
'email1' => "emailOneData",
'email2' => "emaiTwoData",
'email3' => "emaiThreeData",
);
$RoleArray = array(
'role1' => "roleOneData",
'role2' => "roleTwoData",
'role3' => "roleThreeData",
);
Not sure what the best way of splitting the data is though. Also I don't really want to do something like this:
$_POST['email_1']
$_POST['email_2']
$_POST['email_3']
.....
Use instead an $_POST Array.
<form id="input-form" action="" method="post" >
<input id="email_1" name="email[]" />
<input id="role_1" name="role[]" />
<input id="email_2" name="email[]" />
<input id="role_2" name="role[]" />
<input id="email_3" name="email[]" />
<input id="role_3" name="role[]" />
</form>
After this you got an array like this:
echo'<pre>';
print_r($_POST['email']);
print_r($_POST['role']);
echo'</pre>';
Of course you still have to escape your input in dependency of your use.
After this you can use a normal foreach on the array.
$mails=array();
$count=count($_POST['role']);
for($i=0;$i<$count;$i++){
$mails["email_".$i]=$_POST['role'][$i];
}
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
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>
<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