1

I can't get the value of my textfield new_list_id using PHP. Here is my code:

<select id="mymenu" size="1" name='sf_old' class='short_tf'>
    <option >&nbsp;</option>
    <option value="nothing">New Input</option>
    <option value="101">101</option>
    <option value="102">102</option>
</select>

<input type='text' class='short_tf' name='new_sf' id='new_list_id' style='display:none;'/>

<script type="text/javascript">
    var selectmenu=document.getElementById("mymenu")
    selectmenu.onchange=function(){
    var index = selectmenu.selectedIndex;
    if (index == '1') {
        document.getElementById('new_list_id').style.display='inline';
        document.getElementById('mymenu').style.display='none';
    }
}
</script>

Using PHP I can get the value of my SELECT tag mymenu if it is the one selected but I can't retrieve the value of my textfield new_list_id if it is one present and the SELECT tag is hidden.

Problem: the value I get for my textfield is the string "nothing" which is from my SELECT tag. Can't figure out why.

I'm also using the name attribute but it gives me the same result. This is my PHP code:

if(isset($_REQUEST['sf_old'])) {
    $sf=$_REQUEST['sf_old'];
} elseif(isset($_REQUEST['new_sf'])) {
    $sf=$_REQUEST['new_sf'];
} else {
    $sf='';
}
            
echo $sf;
3
  • No PHP code in a PHP tagged question. The irony
    – Alexander
    Jan 8, 2013 at 12:07
  • @Alexander please check also my php code. sorry, i should have included it earlier.thanks Jan 8, 2013 at 12:36
  • Yeah, you had a misunderstanding which you can check below
    – Alexander
    Jan 8, 2013 at 12:55

4 Answers 4

1

Changing the display style property of the element to none will only make it visually hidden. It won't change the fact that it will or won't be sent to the server. So, the first condition is always true and the else clause if never triggered.

You can try an alternative way:

$sf = "";

if(isset($_POST['sf_old']) && isset($_POST['new_sf'])){
  if($_POST['sf_old'] == "nothing"){
    $sf = $_POST['new_sf'];
  } else {
    $sf = $_POST['sf_old'];
  }
}

echo $sf;

I prefer using directly $_POST instead of $_REQUEST.

1
  • You're absolutely right. Thanks for your explanation, it really helped me. Jan 9, 2013 at 8:00
1

From php you have to use the "name" attribute. "new_sf" the id attribute is for client side scripting mostly.

2
  • thanks for the response. i'm also using the "name" attribute but it gives me the same result. i always get the value "nothing" from the <select> tag. Jan 8, 2013 at 12:42
  • @user1957878 even though the select tag is "hidden" the php code still sees it and its value, thats why you always get "nothing" which is the default value of the select option.
    – scartag
    Jan 8, 2013 at 12:51
1

try this:

<input type='text' class='short_tf' name='new_list_id' id='new_list_id' style='display:none;'/>

The name attribute is the one PHP will use to retrieve the value.

2
  • @scartag i'm also using the name attribute but it gives me the same value which is the string "nothing" even though i input new values for my textfield. Jan 8, 2013 at 12:29
  • try receiving it as $_POST['new_list_id']
    – Naryl
    Jan 8, 2013 at 15:40
0

I'm asuming this: You want to get the value from the input field if it's not empty otherwise you want the value from the drop down.

you need to expand on you if statements a little

here is the whole code:

<?php 

$sf='';

if(isset($_REQUEST['sf_old'])){    
    $sf = $_REQUEST['sf_old'];
        if($sf == "nothing")
            if(isset($_REQUEST['new_sf'])){
                $sf = $_REQUEST['new_sf'];
        }

    }

    echo $sf;
    ?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.