How do I set initial image url for a php page?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 4039 Views - Last Post: 07 March 2012 - 06:24 AM Rate Topic: -----

#1 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

How do I set initial image url for a php page?

Posted 29 February 2012 - 07:52 PM

So I have created the following page at my website, please take a quick look to see my two problems listed below:

Portland Climate Data PHP page

Here are my two problems:

1) Upon initial page load I would like a default image to show up rather than seeingthe missing image icon.

2) After selecting a year in the drop down listbox, The value resets to year 2010 every time. How would I preserve the currently selected value when the html page posts back?

I have attached the php file I am using for the form handler.

Feel free to email me the fixed file at the below email:
Brian Schmit Email Here

Thank you very much!!

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: How do I set initial image url for a php page?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: How do I set initial image url for a php page?

Posted 29 February 2012 - 09:49 PM

Fix your path:
/brianschmit/ClimateImages/.png

If you want a selected year to remain selected, you need to put:
selected="selected"
in that option.

If you want help with your code you should post it in code tags as directed practically everywhere on this site.
Was This Post Helpful? 0
  • +
  • -

#3 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 03:03 AM

Ok, I took a wild guess as to where I put the selected="selected" code to keep the value in the drop down from resetting each time but it didn't work. Maybe someone can look at this code and help me. Currently its at line 22. I have no idea what I am doing and learning this PHP is very discouraging.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Portland Climate Data</title>

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

<style type="text/css">
</style>


</head>

<body style="height: 53px; color: #FFFFFF; background-color: #000000;">

<form  action="process.php" method="post">
	<div id="banner" class="banner">
		Portland Oregon Climate Data </div>
	<div id="header" class="header" style="height: 35px; width: 9%">
	<select name="item" style="width: 55px selected="selected">
	<option>2010</option>
<option>2009</option>
<option>2008</option>
<option>2007</option>
<option>2006</option>
<option>2005</option>
<option>2004</option>
<option>2003</option>
<option>2002</option>
<option>2000</option>
<option>1999</option>
<option>1998</option>
<option>1997</option>
<option>1996</option>
<option>1995</option>
<option>1994</option>
<option>1993</option>
<option>1992</option>
<option>1991</option>
<option>1990</option>
<option>1989</option>
<option>1988</option>
<option>1987</option>
<option>1986</option>
<option>1985</option>
<option>1984</option>
<option>1983</option>
<option>1982</option>
<option>1981</option>
<option>1980</option>
<option>1979</option>
<option>1978</option>
<option>1977</option>
<option>1976</option>
<option>1975</option>
<option>1974</option>
<option>1973</option>
<option>1972</option>
<option>1971</option>
<option>1970</option>
<option>1969</option>
<option>1968</option>
<option>1967</option>
<option>1966</option>
<option>1965</option>
<option>1964</option>
<option>1963</option>
<option>1962</option>
<option>1961</option>
<option>1960</option>
<option>1959</option>
<option>1958</option>
<option>1957</option>
<option>1956</option>
<option>1955</option>
<option>1954</option>
<option>1953</option>
<option>1952</option>
<option>1951</option>
<option>1950</option>
<option>1949</option>



	</select>   &nbsp 
		<input name="Submit1" type="submit" value="submit" style="height: 23px"
" /></div>
	<br />
	</form>
	<div id="content" align="center" class="content">
<?php
$item = $_POST['item'];
$url = "/FORMPROCESSING/ClimateImages/$item.png"; 

echo "<img src='$url'>" ;
 ?>
 </div>
</body>
</html>



Was This Post Helpful? 0
  • +
  • -

#4 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 03:12 AM

When I initially run the program, I get this error until I select something from the drop down list. I would like to put in an initial value of "2010" upon loading. See image below..

Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 04:28 AM

<select name="item" style="width: 55px selected="selected">


You didn't close the " on your style, but it's wrong anyway. Where to put the selected="selected"? THINK about it LOGICALLY...what are you selecting?

Your error is because you've not POSTed to the page on initial load, so there is no item in the $_POST array. So, again, think logically. You can test whether an index in an array is set using the isset() PHP function. If this isn't set, think about what it is you want to display and set the $item variable appropriately.

I could give you the answer outright, but you need to LEARN PHP (and HTML), not be handed answers all the time.
Was This Post Helpful? 2
  • +
  • -

#6 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 07:49 AM

Thank you for the hints Jack... I will try to figure this out now. I am going through the Lynda.com PHP training for a second time currently. I am not a student, just trying to produce something on my own so I can get a decent job.
When I was in college we learned vb6 and html which doesn't cut it in today's dynamic web market.
Was This Post Helpful? 0
  • +
  • -

#7 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 09:51 AM

Well I just spent another 90 minutes trying to figure this out. I got the page to show a default image if $item is not set. I tried to figure out where to put the selected="selected" but failed to figure out where to put that code. I am sorry about that. I am still getting an error upon initial load on line #96. I moved that statement all around that block of code but never could get the results I was looking for. Can I have more hints please?

Thank You!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Portland Climate Data</title>

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

<style type="text/css">
</style>


</head>

<body style="height: 53px; color: #FFFFFF; background-color: #000000;">

<form  action="process.php" method="post">
	<div id="banner" class="banner">
		Portland Oregon Climate Data </div>
	<div id="header" class="header" style="height: 35px; width: 9%">
	<select name="item" style="width: 55px"   >
	<option>2010</option>
<option>2009</option>
<option>2008</option>
<option>2007</option>
<option>2006</option>
<option>2005</option>
<option>2004</option>
<option>2003</option>
<option>2002</option>
<option>2000</option>
<option>1999</option>
<option>1998</option>
<option>1997</option>
<option>1996</option>
<option>1995</option>
<option>1994</option>
<option>1993</option>
<option>1992</option>
<option>1991</option>
<option>1990</option>
<option>1989</option>
<option>1988</option>
<option>1987</option>
<option>1986</option>
<option>1985</option>
<option>1984</option>
<option>1983</option>
<option>1982</option>
<option>1981</option>
<option>1980</option>
<option>1979</option>
<option>1978</option>
<option>1977</option>
<option>1976</option>
<option>1975</option>
<option>1974</option>
<option>1973</option>
<option>1972</option>
<option>1971</option>
<option>1970</option>
<option>1969</option>
<option>1968</option>
<option>1967</option>
<option>1966</option>
<option>1965</option>
<option>1964</option>
<option>1963</option>
<option>1962</option>
<option>1961</option>
<option>1960</option>
<option>1959</option>
<option>1958</option>
<option>1957</option>
<option>1956</option>
<option>1955</option>
<option>1954</option>
<option>1953</option>
<option>1952</option>
<option>1951</option>
<option>1950</option>
<option>1949</option>



	</select>   &nbsp 
		<input name="Submit1" type="submit" value="submit" style="height: 23px" 
" /></div>
	<br />
	</form>
	<div id="content" align="center" class="content">

<?php
//The below line throws an error on initial opening of the page	
$item = $_POST['item'];

if(!isset($item)) {
//The following line selects the default of 2010 if the var $item is not set
$item = 2010;
$url = "/FORMPROCESSING/ClimateImages/$item.png";
}
$url = "/FORMPROCESSING/ClimateImages/$item.png";
echo "<img src='$url'>" ;
 ?>
 </div>
</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 11:18 AM

If you'd use PHP to generate HTML you'd find this to be much easier. Of course, you'll still need to know some HTML.
Was This Post Helpful? 0
  • +
  • -

#9 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 11:31 AM

I added in the initial value to the drop down but I am still getting the same error on initial loading. Thank you for that hint. Unfortunately I don't have enough knowledge to figure it out yet.

<body style="height: 53px; color: #FFFFFF; background-color: #000000;">

<form  action="process.php" method="post">
	<div id="banner" class="banner">
		Portland Oregon Climate Data </div>
	<div id="header" class="header" style="height: 35px; width: 9%">
	<select name="item" style="width: 55px"  >
	
<option selected="selected">2010</option>
<option>2009</option>
<option>2008</option>
<option>2007</option>
<option>2006</option>
<option>2005</option>
<option>2004</option>
<option>2003</option>
<option>2002</option>
<option>2001</option>
<option>2000</option>
<option>1999</option>
<option>1998</option>
<option>1997</option>
<option>1996</option>
<option>1995</option>
<option>1994</option>
<option>1993</option>
<option>1992</option>
<option>1991</option>
<option>1990</option>
<option>1989</option>
<option>1988</option>
<option>1987</option>
<option>1986</option>
<option>1985</option>
<option>1984</option>
<option>1983</option>
<option>1982</option>
<option>1981</option>
<option>1980</option>
<option>1979</option>
<option>1978</option>
<option>1977</option>
<option>1976</option>
<option>1975</option>
<option>1974</option>
<option>1973</option>
<option>1972</option>
<option>1971</option>
<option>1970</option>
<option>1969</option>
<option>1968</option>
<option>1967</option>
<option>1966</option>
<option>1965</option>
<option>1964</option>
<option>1963</option>
<option>1962</option>
<option>1961</option>
<option>1960</option>
<option>1959</option>
<option>1958</option>
<option>1957</option>
<option>1956</option>
<option>1955</option>
<option>1954</option>
<option>1953</option>
<option>1952</option>
<option>1951</option>
<option>1950</option>
<option>1949</option>
	</select> 

This post has been edited by Schmit38: 01 March 2012 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#10 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 11:53 AM

Still getting an undefined index error on line #09 after setting the initial value in the drop down. Seems like I am never gonna get this

<?php include("includes/form.php")
?>

	<div id="content" align="center" class="content">

<?php
//The below line throws an error on initial opening of the page	

[size="4"][b]$item = $_POST['item'];[/b][/size]

if(!isset($item)) {
//The following line selects the default of 2010 if the var $item is not set
$item = 2010;
$url = "/FORMPROCESSING/ClimateImages/$item.png";
}
$url = "/FORMPROCESSING/ClimateImages/$item.png";
echo "<img src='$url'>" ;

 ?>
 </div>
</body>
</html>


This post has been edited by Schmit38: 01 March 2012 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 12:28 PM

$_POST is empty on the first load. Read up on isset.
Was This Post Helpful? 0
  • +
  • -

#12 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: How do I set initial image url for a php page?

Posted 01 March 2012 - 01:08 PM

I realize that. If I populate it at the beginning of the Php it doesnt change when I select a different year. I have spent about 3+ hours on this one thing.
I guess I will walk away from it now. I wish there was a line-by-line debugger like Visual Studio in PHP.

Now the error is gone but I can only get 2010... Grr.. So frustrating

?php include("includes/form.php")
?>

	<div id="content" align="center" class="content">

<?php
//The below line throws an error on initial opening of the page	
$_POST['item'] = 2010;

$item = $_POST['item'];

if(!isset($_POST['item'])) {
$_POST=2010;
$item = $_POST['item'];
}

if(!isset($item)) {
//The following line selects the default of 2010 if the var $item is not set
$item = 2010;
$url = "/FORMPROCESSING/ClimateImages/$item.png";
}
$url = "/FORMPROCESSING/ClimateImages/$item.png";
echo "<img src='$url'>" ;

 ?>
 </div>
</body>
</html>


This post has been edited by Schmit38: 01 March 2012 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#13 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: How do I set initial image url for a php page?

Posted 02 March 2012 - 05:11 AM

Hi,

Well you have this line at the top
$_POST['item'] = 2010;



so no matter what you actually post to the file you will always overwrite it with the value 2010.
Was This Post Helpful? 0
  • +
  • -

#14 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: How do I set initial image url for a php page?

Posted 02 March 2012 - 01:35 PM

I went as studied the isset() function and have tried many different code configs of which was no help. Here is my current code. Now setting the current list box item to 2010 removed the error upon loading the page but now I can't access another year. When I select another year the drop down just goes back to 2010. For some reason the var dump shows false every time.

<?php include("includes/form.php")
?>
<?php
//Check to see if $_POST is null
If(!isset($_POST['item'])); {
$_POST['item'] = 2010;
$item = $_POST['item'];
}
//Shows if not set is true or false
var_dump(!isset($_POST['item'])); {
}

//The following line sets URL and echoes image to browser
$url = "/FORMPROCESSING/ClimateImages/$item.png";
echo "<img src='$url'>" ;
 ?>
 </div>
</body>
</html>


I must say I got lots of help in the past on here with vb.net problems. It seems that some people are reluctant to help me here in the php forum. Not sure why that is but I am just about ready to give up on this. I have spent probably 5 hours looking at this code and researched and NOTHING gets solved.

Here is my includes page: This is where I set the default selection to "2010" for the drop down.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Portland Climate Data</title>

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

<style type="text/css">
</style>

</head>

<body style="height: 53px; color: #FFFFFF; background-color: #000000;">
	<div id="content" align="center" class="content">
<form  action="process.php" method="post">
	<div id="banner" class="banner">
		Portland Oregon Climate Data </div>
	<div id="header" class="header" style="height: 35px; width: 9%">
	<select name="item" style="width: 55px"  >
	
<option selected="selected">2010</option>

<option>2009</option>
<option>2008</option>
<option>2007</option>
<option>2006</option>

	</select> 

	&nbsp 
		<input name="Submit1" type="submit" value="submit" style="height: 23px">
 </div>
	<br />
	</form>


Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: How do I set initial image url for a php page?

Posted 02 March 2012 - 02:45 PM

Here's a question for you. How is the code below strongly related to what you're trying to do?
<?php
$data = array("Hello ","Goodbye ");
$x = rand(0,1);
echo str_replace("{A_tag}",$data[$x], file_get_contents("world.html"));
?>


world.html:
<h1>{A_tag}World!</h1>

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3