Using Variable Inside $_POST[ ] - php

How to use variable inside $_POST as i have used the following code but it says undefined offset in second line. How can i solve it?
$p = $_GET['cii'];
$selectOption = $_POST[$p];
Below is the code:
echo'<form method ="POST">
<select name="'.$abc[3].'">
<option value="slow">slow</option>
<option value="medium">medium</option>
<option value="fast">fast</option>
</select>
<br>
Click to change
</form>';
$abc[] has some numbers and the select box is made with same name as the number.

Almost there:
$p = 'cii';
$selectOption = $_POST[$p];

you are accessing same page when you come first time you get undefined offset
So first check existence
if(isset($_GET['cii'])){
$p = $_GET['cii'];
$selectOption = $_POST[$p];
}
Also no need to add double && just use single & here
Click to change
Assuming variable $abc[3] gives you right value.

Related

serialize function is not working in php

Hi i get the value from post i used serialize function its showing wrong value. Its just displaying N.. pls help me
Php Code
$item_select_alpha = $_POST['item_select_alpha'];
for ($alpha = 1; $alpha <= count($item_select_alpha); $alpha++) {
$serialise = serialize(array($item_select_alpha[$alpha]));
}
$item_quanity = $_POST['item_quanity'];
for ($qty = 1; $qty <= sizeof($item_quanity); $qty++) {
$item_quan = serialize($item_quanity[$qty]);
}
print_r($item_quan);
exit;
HTML Code
<select class="item_select_alpha" name="item_select_alpha[]">
<option value="">select the Alphabetic</option>
{foreach $size_alpha as $sa}
<option value="{$sa['size_id']}">{$sa['size_name']}</option>
{/foreach}
</select>
<input type="text" class="item_quanity" name="item_quanity[]" class="form-control">
You not add items to an array, but only changes the variable so it contains the last element. Try this:
$serialise[] = serialize(array($item_select_alpha[$alpha]));
$item_quan[] = serialize($item_quanity[$qty]);
your variables in for loops will contains just the last element, you update their value at each iteration. $item_quan defined in the second loop won't be "printable" out of the loop...

Can't make php conditional "if" work inside HTML

my code is the following;
<select class="reg_field_field" id="user_address_state" name="user_address_state" tabindex="7">
<option value="AL" <?php if($state=='AL') echo 'selected';?>/>Alabama</option>
<option value="AK" <?php if($state=='AK') echo 'selected';?>/>Alaska</option>
<option value="AZ" <?php if($state=='AZ') echo 'selected';?>/>Arizona</option>
....
</select>
And the result is showing me instead the state name, it show "Notice: Undefined variable...".
I tried this in other server and worked, could be the php.ini configuration???
What can be on php.ini?
Thank you for any help
Ale
The error tells you the problem: $state is undefined. You need to examine your code to determine where $state should be defined and ensure that it is being set properly.
If you are attempting to save user input to the $state variable, look for a line like:
$state = $_POST['user_address_state'];
If it does not exist, create it prior to the lines you included in your post.
does $state have a default value? It has to have a value prior to checking to see if it matches against another.

How to get the value and id from a drop down in php?

I have created and html form which have a drop down list.
This drop down list is populated from database.
<select name="classes">
<?php
foreach() {
?>
<option value="<?php echo $id ?>"><?php echo $name ?></option>
<?php
}
?>
</select>
Now I want to get the $id and $name both. How will I do this?
I have tried this
echo $_POST['classes'];
But it only displays the $id of the select item. And I want $id and $name both.
You can't. One possibility would be placing both infos inside the value attribute, and then separating them back again with php (by using a delimiter):
<option value="<?php echo $id.'|'.$name; ?>"><?php echo $name ?></option>
In PHP:
$datas = explode('|',$_POST['classes']);
$id = $datas[0];
$name = $datas[1];
But that's not how the system is meant to be. Usually the $name would be used only as a "friendly" info for the user, cause the value might sometimes just be an INT and user won't understand what that int refers to, so we give him a word description in order to choose an option: but what you would only care of is that value indeed, which you can always use to get again the description that comes along with it (by a search to the database, for ex.)
As far as I know, when you submit that form, only the value is going to be carried over. If there is no value then the inner HTML becomes the value.
What I'd do is:
<select name="classes">
<?php
foreach($classes as $id => $name) //i'm guessing here, is this what you meant?
{ ?>
<option value="<?php echo $id.'|'.$name ?>"><?php echo $name ?></option>
<?php } ?>
</select>
In case you are not familiar, the period is the concatenation operator. Think of it as glue for pieces of a string. So I'm gluing $id to the left of "pipe" and then gluing $name onto the end of that.
Then in your handler, use split or explode to separate the two values on the pipe.
Actually, I'd do it a little different, echoing more and going in and out of php/html less, but I tried to leave your code intact as much as possible.
append name to Id and pass it as value,,and explode name from the other end
You want is and name both so while storing option value ,
put like this
<option value="<?php echo $id."_".$name;?>"><?php echo $name?></option>
and on posting data just explode the value you will get both is and name
Sending form means only sending 'value' for select field (treat 'name' in option as a label only). You can simply fetch 'name' from database when you have 'id' while handling form submission.

Get Text From <option> Tag Using PHP

I want to get text inside the <option> tags as well as its value.
Example
<select name="make">
<option value="5"> Text </option>
</select>
I used $_POST['make']; and I get the value 5 but I want to get both value and the text.
How can I do it using PHP?
In order to get both the label and the value using just PHP, you need to have both arguments as part of the value.
For example:
<select name="make">
<option value="Text:5"> Text </option>
</select>
PHP Code
<?php
$parts = $_POST['make'];
$arr = explode(':', $parts);
print_r($arr);
Output:
Array(
[0] => 'Text',
[1] => 5
)
This is one way to do it.
What about this? I think it's the best solution because you have separated fields to each data. Only one hidden field which is updated at each change and avoids hardcoding mappings.
This inside HTML:
<select name='make' onchange="setTextField(this)">
<option value = '' selected> None </option>
<option value = '5'> Text 5 </option>
<option value = '7'> Text 7 </option>
<option value = '9'> Text 9 </option>
</select>
<input id="make_text" type = "hidden" name = "make_text" value = "" />
<script type="text/javascript">
function setTextField(ddl) {
document.getElementById('make_text').value = ddl.options[ddl.selectedIndex].text;
}
</script>
This inside PHP:
<?php
$value = $_POST["make"];
$text = $_POST["make_text"];
?>
set the value of text to the value of the option tag, be it through static HTML markup or even if it's being generated by a server side script. You will only get the value attribute through POST
Another option however, on the server side, is to map the value ("5"), to an associative array, i.e.
<?php
$valueTextMap = array("5" => "Text");
$value = $_POST['make']; //equals 5
$text = $valueTextMap[$value]; //equals "Text"
?>
You'll need to include that Text in the value to begin with (e.g.: <option value="5_Text"> Text </option> and then parse, or...
You could use javascript on the page to submit the text as another parm in the POST action.
You can use simple dom
<?php
include('/mnt/sdb1/addons/simplehtmldom/simple_html_dom.php');
$html = file_get_html('tari.html');
$opt = array();
foreach($html->find('option') as $a) {
$opt[] = $a->value;
}
print_r($opt);
?>
I have always used a very elegant solution, similar to the ones already presented, which does not require a lot of additional code.
HTML
<select name="make">
<option value="1:First Option">First Option Text</option>
<option value="2:Second Option">Second Option Text</option>
<option value="3:Third Option Text">Third Option Text</option>
</select>
PHP
$value = split(':', $make)[0];
$text = split(':', $make)[1];
Benefits of this method
Yes, there are definitely similarities to serialworm's answer, yet we minimize the code in our PHP block by inconspicuously converting to an array and picking the element required right away.
In my case, I use this exact short-hand code in a contact form where this one-liner (to get the selected department name) is critical to keeping the code looking clean.

Select List with a NULL that gets committed in SQL

I have a select list like the following inside of a form.
<select name="0[voicemail]" >
<option value="" selected="selected"></option>
<option value="800">800</option>
<option value="801">801</option>
<option value="802">802</option>
<option value="803">803</option>
<option value="805">805</option>
<option value="807">807</option>
<option value="808">808</option>
<option value="809">809</option>
<option value="810">810</option>
<option value="811">811</option>
<option value="820">820</option>
<option value="830">830</option>
<option value="831">831</option>
<option value="9778">9778</option>
<option value="9995">9995</option>
</select>
This was generated by some Kohana PHP code.
$id = 0;
$disabled = '';
foreach ( $line_detail as $line ){
echo '<tr class="d'.($id & 1).'">';
echo '<td>'.$line->did.form::hidden($id."[did]", $line->did).'</td>';
echo '<td>'.form::input($id."[cid_prefix]", $line->cid_prefix).'</td>';
echo '<td>'.$line->type.'</td>';
echo '<td>'.form::input($id."[ivr_context]", $line->ivr_context, "disabled='true'").'</td>';
if ($line->ivr_context != ''){
$disabled = "disabled='true'";
echo '<td>'.form::input(array('name'=>$id."[dial_timeout]", 'size'=>15,
'maxlength'=>3), $line->dial_timeout, $disabled).
form::hidden($id."[dial_timeout]", $line->dial_timeout).'</td>';
echo '<td>'.form::dropdown($id."[voicemail]", $phones, $line->voicemail, $disabled).
form::hidden($id."[voicemail]", $line->voicemail).'</td>';
} else {
echo '<td>'.form::input(array('name'=>$id."[dial_timeout]", 'size'=>15,
'maxlength'=>3), $line->dial_timeout, $disabled).'</td>';
echo '<td>'.form::dropdown($id."[voicemail]", $phones, $line->voicemail, $disabled).'</td>';
}
echo '<td>'.form::input($id."[notes]", $line->notes).'</td>';
echo "</tr>";
$id++;
}
Not everything shown, but basically the options are in the $phones variable.
Now the problem.
When I use a form submit all is fine until I choose the empty value in the submit.
This is inside a method where $detail is equivalent to $_POST
foreach($detail as $key => $val){
$this->db->query("UPDATE dids SET cid_prefix=?, dial_timeout=?, voicemail=?, notes=? WHERE did=?",
array($val['cid_prefix'],
$val['dial_timeout'],
$val['voicemail'],
$val['notes'],
$val['did']));
The problem here is that I set the value for the empty option to be "NULL", but because Kohana adds commas around everything it inserts it'll try to put "NULL" into the database instead of NULL. In this case this will violate a foreign key constraint.
Is there a simple way to deal with NULL in PHP/Kohana so that I don't have to check for blank and rewrite each query that might contain NULL more than one time.
What happens when you could get multiple valid NULLs? Surely there is a way to deal with these types of situations simply?
Placing this code before the database update seems to fix the problem. I.e. set $val['voicemail] to the PHP constant NULL.
if (empty($val['voicemail'])){
$val['voicemail'] = NULL;
}
Then I can continue to use the original SQL statement. Kohana seems to behave itself and NULL is set through in the update statement.
You can use the DB::expr method which will not escape the value in the query. That way you insert a raw NULL
However, I'm amazed you've used a object orientated framework and yet chucked out all the advantages by creating a mess of a form. Job security?

Categories