Entiranz Posted March 28, 2012 Share Posted March 28, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/259861-using-a-php-variable-as-a-html-form-name/ Share on other sites More sharing options...
seanlim Posted March 28, 2012 Share Posted March 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259861-using-a-php-variable-as-a-html-form-name/#findComment-1331827 Share on other sites More sharing options...
Entiranz Posted March 28, 2012 Author Share Posted March 28, 2012 Thanks alot, that answers my question. I cant say im happy with it, since im not proficient in javascript, so i guess ill have to settle for the next best thing then. I can live with that i guess. Thanks again though. Quote Link to comment https://forums.phpfreaks.com/topic/259861-using-a-php-variable-as-a-html-form-name/#findComment-1331838 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.