Jump to content

using src img code in a form


senca99

Recommended Posts

hey,

I made the following (using only listmenu with 1 option just to test):

 

an index.php:

<html>

<link href="oefening.css" rel="stylesheet" type="text/css">

<body>

 

<div id="wrapper">

<div id="links">

<form action="index.php" method="get">

  <p>

    <label>selecteer hier iets

      <select name="menu" id="menu">

        <option value= "test">  Naam </option>

</select>

    </label>

  </p>

  <p>

    <input type="submit"/>

  </p>

</form>

</div>

<div id="rechts">

 

<? include("script.php"); ?>

 

</div>

 

</div>

 

</body>

</html>

 

and a php.script:

 

<html>

<body>

 

<? echo $_GET['menu']; ?>

 

</body>

</html>

 

This works perfect.But now I want to print on screen an image instead of the word test and I've tried on different ways using the img src code inside the form but it never works:s. Anyone know how to make the image appear instead of the word?

 

thx a lot in advance!

Link to comment
https://forums.phpfreaks.com/topic/179351-using-src-img-code-in-a-form/
Share on other sites

it depends where you want it to be. There are a few stuff you should correct before you decide where to place it:

 

1. script.php need not have <html> and <body> tags. The output from script.php will just replace <? include("script.php"); ?>. You don't usually have <html> and <body> tags within a DIV, do you?

 

2. You should test whether the form has been submitted before trying to print the variable $_GET['menu']:

 

if(isset($_GET['menu'])){ echo $_GET['menu']; }

 

 

 

If you want the image to display only when the user submits the form, then place the image tags WITHIN the curly brackets.

 

 

Indeed the image should only be displayed when the user presses the submit button,then the code should like this? ==>

 

if(isset($_GET['menu']))

{ echo $_GET['menu'];

<img src="...url here..." alt="...">}

 

and I have to put this in script.php? Because I tried it and I get errors in the div where the image is supposed to be shown.

works like a charm exept that there is always the label posted together with the image.But thats not really an issue.

I've been thinking of expanding the code and give the user more options in the menu (multiple pictures to choose from) but I don't know how to get the outcome to be the selected image.I thought about copying the code in script.php and just change the name of the image but then it displays the 2 images because the script refers to the entire form.I don't really have a solution for this one :s

use the following if you want to "get the outcome to be the selected image", without the label.

 

<?
if(isset($_GET['menu']))
{
echo "<img src=\"".$_GET['menu'].".jpg\" alt=\"Some Text\">";
}
?>

 

If you want multiple images, you will have to use a combo box which allows for more than one option to be selected. but that isn't what you started this topic for :P

ok, i'll point you in the right direction... your SELECT must have a MULTIPLE attribute: <select name=".." multiple>

 

when you submit the form, the selected options will be in an array. i believe you will be able to find help on handling and looping through an array online. for each iteration, just print out the <img> tag for each option selected.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.