Using a dropdown box to change PHP include() value

  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

Hi All,

I'm trying to figure out a way to have a user use a drop down menu and when they select an item the information they need shows up under the drop down menu as a php include. This is what I have so far but alas it doesn't seem to work. Any suggestions would be greatly appreciated!

Code: [ Select ]
<body>
<form name="form1" method="POST" action="formtest.php">
<select name="select" onChange="document.form1.submit()">
 <option value="s1">Selection1</option>
 <option value="s2">Selection2</option>
</select>
</form>

<?php
$file = $_POST['select'];

require_once($_SERVER['DOCUMENT_ROOT']/$file.php);

?>

</body>
  1. <body>
  2. <form name="form1" method="POST" action="formtest.php">
  3. <select name="select" onChange="document.form1.submit()">
  4.  <option value="s1">Selection1</option>
  5.  <option value="s2">Selection2</option>
  6. </select>
  7. </form>
  8. <?php
  9. $file = $_POST['select'];
  10. require_once($_SERVER['DOCUMENT_ROOT']/$file.php);
  11. ?>
  12. </body>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Does the form not submit when you change the selection? I'm not quite sure what your current problem is.
  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

Well so far the form value is passing to the $file but the include file isn't showing up
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Try adding something like this:

PHP Code: [ Select ]
$path_file = $_SERVER['DOCUMENT_ROOT'] . "/" . $file . ".php";
if(!file_exists($path_file))
{
   echo("The file does not exist at: " . $path_file);
}
else
{
   require_once($path_file);
}
 
  1. $path_file = $_SERVER['DOCUMENT_ROOT'] . "/" . $file . ".php";
  2. if(!file_exists($path_file))
  3. {
  4.    echo("The file does not exist at: " . $path_file);
  5. }
  6. else
  7. {
  8.    require_once($path_file);
  9. }
  10.  
  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

I tried putting that in but it didn't change anything and doesn't even echo "the file does not exist" Although in my code below it does echo $file so it's seeing the drop down values....

Code: [ Select ]
<body>
<form name="form1" method="POST" action="formtest.php">
<select name="select" onChange="document.form1.submit()">
 <option selected>Select an Industry</option>
 <option value="s1">Selection1</option>
 <option value="s2">Selection2</option>
</select>
</form>

<?php

$file = $_POST['select'];
echo $file;


$path_file = $_SERVER['DOCUMENT_ROOT']/$file.php;
if(file_exists($path_file))
{
  echo("The file does not exist at: $path_file");
}
else
{
  require_once($path_file);
}?>
</body>
  1. <body>
  2. <form name="form1" method="POST" action="formtest.php">
  3. <select name="select" onChange="document.form1.submit()">
  4.  <option selected>Select an Industry</option>
  5.  <option value="s1">Selection1</option>
  6.  <option value="s2">Selection2</option>
  7. </select>
  8. </form>
  9. <?php
  10. $file = $_POST['select'];
  11. echo $file;
  12. $path_file = $_SERVER['DOCUMENT_ROOT']/$file.php;
  13. if(file_exists($path_file))
  14. {
  15.   echo("The file does not exist at: $path_file");
  16. }
  17. else
  18. {
  19.   require_once($path_file);
  20. }?>
  21. </body>
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Sorry, I wrote the code wrong initially, you can find the updated code in the post above though.
  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

Thanks!
I now get the "the file does not exist" echo. So now I know that my files are not in the right place. How would you change $SERVER['DOCUMENT_ROOT'] to something that would mean the file is in the same place as initial file that is being read?

Thanks so much for you help by the way
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

If you want to include a file in the same directory as the file you are viewing you can simply just use the file name. However this isn't the best practice because it will make your site very insecure. Your best option is to continue to use document root, but to correct the error in the path.

For now though, you can simple use the following to get a file in the current directory of the file your viewing.

PHP Code: [ Select ]
require_once("./" . $file);
 
  1. require_once("./" . $file);
  2.  
  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

perfect. Everything is now working. Thanks

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: Liamw411 and 37 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.