Page 1 of 1

Specific name typed, specific image shown on html page

Posted: Thu Oct 22, 2009 4:46 pm
by sixseven67
Hello,

Fairly new to PHP and was curious if someone might know how to solve a fairly simple request.

I am currently passing the name of an end user (who types their name in a form field) to a URL which is then passed along and placed into an html page to personalize the page for them to print. Is there a way that I could use PHP to use a specific image for a specific name typed in the field?

Example would be:

Name typed in field: Bob
Picture used: Baseball

OR

Name typed in field: Alison:
Picture used: Cat

Below is some of the coder snippets I am using.



Here is the code from the form that the end user types in their name:

<form id="form1" name="form1" method="post" action="name_of_user.php">

<label>Gallery Name:<br />
<input type="text" name="name" id="name" />
</label>
<br />
<label>
<input type="submit" name="Submit" id="Submit" value="Submit" />
</label>
</form>

Here is the code that receives the post from above file:
<?php

$Name = Trim(stripslashes($_POST['Name']));

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=path to file/comp_pass_exhibitor.php?name=$Name\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=path to file/comp_pass_exhibitor.php?name=$Name\">";
}
?>


________Below is code from the html page I want personalised_______

Here is the bit of code that places the name in the html page:
<p><strong>
<span style="text-transform:uppercase;"><?php echo $_GET['name']; ?></span> <br>
some text that doesn't change</strong> </p>

Here is the bit of code for the image on the html page:

<td>
<img src=" What would go here " width="140" height="109" align="left">
</td>



Any help would be appreciated

Re: Specific name typed, specific image shown on html page

Posted: Thu Oct 22, 2009 6:49 pm
by Weiry
Well for your example, which ONLY includes 2 names, a simple if statement at the top of your page like

Code: Select all

 
if($_GET['name']=="Alison"){
   $userImage = "cat.jpg"
}elseif($_GET['name']=="Bob"){
   $userImage = "baseball.jpg";
}
....
 
But this is ONLY good for the users you specify which is bad programming practice. But i need to ask,
How are you telling which image goes with which name?
Im just assuming you dont have only 2 names which you can type into your field.

If your using a database, what sort of information are you storing in order to determine which image they are using?
If so, then i suggest running a query to return the image based on the input name.

Code: Select all

SELECT `image` FROM `users` WHERE `name` = '{$_GET['name']}'
If you can't use a database, then i suggest creating an associative array to handle the information.

Code: Select all

$userArr = array( array("name" => "Bob", "image" => "baseball.jpg"), array("name" => "Alison", "image" => "cat.jpg"));
foreach($userArr as $user){
   if($user['name'] == "{$_GET['name']}"){
      $userImage = $user['image'];
   }
}
Finally, you would then (in your image src field) insert the appropriate variable along with the path to the image.

Code: Select all

<img src="images/<?php print $userImage;?>" width="140" height="109" align="left">

Re: Specific name typed, specific image shown on html page

Posted: Thu Oct 22, 2009 7:41 pm
by sixseven67
Thank you for your reply Weiry. I didn't see the guidelines before I posted, but will keep that in mind for future posts.

Unfortunately I don't have much experience with databases.

How are you telling which image goes with which name?
Still trying to figure that out. The more I researched the more I was thinking an array would work. Each name would have only one image assigned to it. When they type their name into the name field and hit "submit," it would somehow pass that info on and place the correct image in the img src. I would store these images in a separate folder on the server.

When setting up the array code, would I place that in the same php file that is already grabbing the "name" from my sample code? The current code pulls the name they typed in the form from the url and places it on the html page to personalize it.

Thanks