Storing the value of a drop box as a variable name

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

adke001458
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Aug 11, 2014 12:50 am

Storing the value of a drop box as a variable name

Postby adke001458 » Mon Aug 11, 2014 12:58 am

Hi All,

I'm a newbie to coding and php and have been trying to solve this problem for hours, so if someone could help me it would be greatly appreciated.

To simplify the situation, essentially I have a drop down box on a web page. This drop down box automatically fills with values that are the result of a query (on an oracle DB).

What I am trying to do is save the value of the dropbox at any moment into a variable. I want the variable name to be the actual value.

For example, lets say the dropbox contains A, B, C, D. If A is selected, I want the variable to be named A.

Here is the code that I have.

$conn = oci_connect(....);

if ($conn {

$query = "Select.......";
$stid = oci_parse($conn, $query);
$success = oci_execute($stid);


?>

<form name="searchform" method="post" onsubmit="return check_my_form()">
<table align="center" bgcolor="#666666" border="0" cellpadding=0 width="100%">
<tr><td align="left">
<table align="left" bgcolor="#666666" cellpadding=2 border="0" STYLE="font-family:Arial;font-size : 0.8em">
<tr><td><font Color=white>Select Task : </font></td>
<td><select name="taskKey" STYLE="font-family:Arial;font-size : 0.8em" onChange="this.form.submit();">


<?php
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_BOTH))
echo "<option value= '".$row[0]."'>" .$row[0]. "</option>";
?>

<?php
} //end conn
$myTaskKey = $_POST["taskKey"];
echo $_POST["taskKey"];


-----------
the echo is showing as blank.

Please help.

Thanks
Adam
?>

landi
php-forum Active User
php-forum Active User
Posts: 303
Joined: Thu Mar 15, 2012 3:59 pm

Re: Storing the value of a drop box as a variable name

Postby landi » Mon Aug 11, 2014 1:49 am

Hi Adam,
Welcome to PHP.

PHP + Oracle is a good combination IMHO!

I can't see anything obviously wrong with your code. I'm assuming you've omitted bits for clarity.
I've recreated it below (without the DB), and this works for me:

Code: Select all

<html>
<head>
   <title>title</title>
</head>
   <body>
      <form name="searchform" method="post" onsubmit="return check_my_form()">
         <select name="taskKey" onChange="this.form.submit();">
         <?php
            $row = array('A');
            echo "<option value= '".$row[0]."'>" .$row[0]. "</option>";
            $row = array('B');
            echo "<option value= '".$row[0]."'>" .$row[0]. "</option>";
         ?>
         </select>
      </form>
      <?php
      echo 'S=' . $_POST["taskKey"];
      ?>
   </body>
</html>

When displaying the form for the first time, $_POST["taskKey"] is of course undefined. When I select 'B', the form submits and prints S=B

I want the variable name to be the actual value.
This is an unusual request. Maybe you meant what I've shown above, or did you mean that if you select B you want to store the value 'B' in a variable named $B? It's probably possible, but not entirely useful.

-A

adke001458
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Aug 11, 2014 12:50 am

Re: Storing the value of a drop box as a variable name

Postby adke001458 » Mon Aug 11, 2014 2:18 am

Thanks for your response!

I understand where I was going wrong now. - I had a line of code somewhere basically preventing it from working, and also I didn't realise I had to change the dropdown box value for it to work. So originally it was always showing as blank.

QQ then - when i change the dropdown value (for example from A to B) once refreshed it loads back to its original value (A).

How do I keep it showing as B?

In addition, how do I make sure that once the page loads, it automatically saves 'A' into the variable; the first dropdown box option instead of it not registering anything.

Thanks a lot,
Adam

landi
php-forum Active User
php-forum Active User
Posts: 303
Joined: Thu Mar 15, 2012 3:59 pm

Re: Storing the value of a drop box as a variable name

Postby landi » Mon Aug 11, 2014 3:33 am

Hi,
How do I keep it showing as B?

1. You need to add a 'selected' attribute to the desired option (this is an HTML requirement, not PHP) (more below)

how do I make sure that once the page loads, it automatically saves 'A' into the variable

2. By a simple assignment into whatever variable you use for #1 above.

The implied question is more philosophical: How does one write a page that displays empty (or default=A) inputs the first time, and on the second and subsequent passes, displays the values the user entered?

The answer I use is:
1) On your page add a hidden field called 'IsSubmitted' and give it a value 'Y'.

2) At the top of your page, check if IsSubmitted is set:
a) if not, the page is being displayed for the first time. Set default e.g. $sTaskKey = 'A';
b) if set, load the form values into variables (#2 above) e.g.
$sTaskKey = $_REQUEST['TaskKey']; // You need to do a bit more checking in case not set

3) When you display the options of the select, check each option value against $sTaskKey, and if the same, add the word 'selected' into that option (#1 above)

As you can see the whole 'page cycle' thingy is quite complex. If you are going to write more than the odd page you will want to either use a framework (sorry, no ideas, but post a new question for suggestions), or write a bunch of classes yourself to handle the various components.

-A


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests