Jump to content

Using a PHP variable as a HTML Form Name


Entiranz

Recommended Posts

Okay, im trying to make this part of a webpage dynamic, so that a select option grants a value to a PHP variable inside the same file. That way, the variable will control a certain part of the file output so that it controls another select option. That select option will give variables to another file, but lets focus on this part.

 

The reason why is, the only other way i can see that would work is having either loads of options in one select bar or four select bars with options each (i have four tables in the database im trying to connect to). Lets focus on this part of code below.

 

<?php
$Sokval;
$Sokval1;
echo "<select name=\"$Sokval1\">";
echo "<option>anställda</option>";
echo "<option>avdelningar</option>";
echo "<option>kontakt information</option>";
echo "<option>privat information</option>";
echo "</select>";
$Sokval = $Sokval1;
?>

 

Two Examples of different options that would pop up as the select bar.

<?php
if($Sokval == "anställda") {
  echo "Tabell Anställda<br />";?>
<select name="Sokval2">
<option>ID</option>
<option>Förnamn</option>
<option>Efternamn</option>
<option>Lön</option>
<option>PI ID</option>
<option>Kontakt ID</option>
<option>Avdelning ID</option>
</select>
<?php } ?>

<?php
if($Sokval == "avdelning") {
  echo "Tabell Avdelningar<br />";?>
<select name="Sokval2">
<option>ID</option>
<option>Avdelning</option>
<option>Syfte</option>
<option>Antal Medlemmar</option>
<option>Effektivitet</option>
</select>
<?php } ?>

 

I don't know if this kind of syntax would work, please let me know if it wouldnt.

 

(And sorry if you dont understand what the options say)

Link to comment
Share on other sites

This is possible with PHP, only if you do not mind a page load/form submission in between. PHP is a server-side script; in general, content sent to the user's browser cannot be changed once it is sent.

 

If you do not mind a form submission and the page reloading between the selection of the first box and the "appearance" of the second box, this method would work fine. However, in the second portion of code, you should be writing:

 

if($_POST['Sokval'] == "anställda") {

 

instead (or GET depending on your form submission type), as you are reading from a form submission.

 

If you do not want a form submission between them, i.e. when you select the first box, the second box automatically appears without the page reloading, PHP will not be enough. You will have to look at a client-side language like Javascript.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.