absolute beginer,please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingkol
Forum Newbie
Posts: 4
Joined: Mon Nov 08, 2010 12:24 am

absolute beginer,please help

Post by kingkol »

hi

I am very new to php and stuck at very basic problem.my goal user will input value through drop down list, and according to that selection nos of images will be display on another div.here is code

Code: Select all

<div id="left_col">
  how many pictures
	<form method = "POST" name="how" action="practice.php">
  	<select name='one'>   
              <option>10</option>
              <option>20</option>
              <option>30</option>	
        </select>
   <input type="submit" name ="submit">
   </form>
   </div>
  <div id="right_col">
  <?php  
 	if ($_POST['submit'])
  	{
		  $how =  $_POST['one'];
		  echo $how;
		  echo "<br>";
	  
	  	for($i=1; $i<=$how; $i++)
	  	{
		  echo "<img src =\"one.jpg\">";
	  	}
  	}
	
  ?>
problem is when for the first time the page loaded its shows
Notice: Undefined index: submit in C:\wamp\www\test\practice.php on line
whats wrong?Why the "if" is not working?

Please help
Thanks in advance
kingkol
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: absolute beginer,please help

Post by cpetercarter »

You are getting the error message because, the first time you load the page, there are no POST values and therefore $_POST['submit'] does not exist. if($_POST['submit'] tests whether $_POST['submit'] has a positive or a negative value; it can't cope with a situation in which $_POST['submit'] does not exist. That is why it is normal to test whether a $_POST variable is set as well as its value

Code: Select all

if (isset($_POST['something'] && $_POST['something'] == some value)
.
In your case, the simplest thing to do would be to use the function empty(). empty() returns true if a variable has a value of false or zero or null, and also if the variable does not exist. So:

Code: Select all

if(!empty($_POST['submit']))
{
//do something
}
kingkol
Forum Newbie
Posts: 4
Joined: Mon Nov 08, 2010 12:24 am

Re: absolute beginer,please help

Post by kingkol »

Thank you for reply.Yes with "isset()" its working fine.Here comes the 2nd problem

when user input - other than the 1st value from the drop down list, it come back with the 1st value(which i believe is default) with the reloaded page.How to show (or select that option )which user have chooses on the reloaded page?

mean if user choose 20, on the reloaded page it shows 20 in the drop down box.

thanks once again.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: absolute beginer,please help

Post by McInfo »

For each of the options, if the submitted value matches the option value, add a "selected" attribute to the option tag. The resulting HTML will look something like this:

Code: Select all

<option>10</option>
<option selected="selected">20</option>
<option>30</option>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: absolute beginer,please help

Post by McInfo »

I rarely write an option tag without the value attribute for my own code, but if there is no value attribute, the value of the option defaults to the option label (the part between the tags).
Post Reply