Jump to content

select from the list


bal bal

Recommended Posts

You should have a form tag that covers that select tag with options. Then have a submit button. Given that you used POST, you can grab it using $_POST['name_of_the_select_tag']. It should throw whatever selected value.

 

If what you are getting at is select something that is non existing... then add another select value that serves as default and check that if it has not selected any other.

 

If you are a beginner, here are some tutorials you can check.

 

http://www.w3schools.com/PHP/DEfaULT.asP

http://www.php.net/tut.php

http://www.tizag.com/phpT/

 

Link to comment
Share on other sites

What do you mean?  If you have 3 things in the list, only those 3 things are available to be selected...  Just make the first one selected by default.

these are in menu bar, when I click on the menu, these 3options will come but I've to select only 1. so how can I validate it if I don't select any or if I select any one of them...

 

Link to comment
Share on other sites

It's actually quite basic PHP, here's the solution:

 

<?php
if ($_POST) {
	echo $_POST['list'];
}
?>

<html>
<head>
</head>
<body>
<form action="thisfile.php" method="post">
<select name="list">
<option value="car">car</option>
<option value="bus">bus</option>
<option value="motorbike">motorbike</option>
</select>
<br>
<input type="submit" name="submit" value="Submit!">
</form>
</body>
</html>

Link to comment
Share on other sites

It's actually quite basic PHP, here's the solution:

 

<?php
if ($_POST) {
	echo $_POST['list'];
}
?>

<html>
<head>
</head>
<body>
<form action="thisfile.php" method="post">
<select name="list">
<option value="car">car</option>
<option value="bus">bus</option>
<option value="motorbike">motorbike</option>
</select>
<br>
<input type="submit" name="submit" value="Submit!">
</form>
</body>
</html>

 

I tried with this code....

 

<?php

if (trim($_POST['list'])<="" { $error = "Please pick from the list";
else    
echo "You selected ". " " . $_POST['list'] . ".<br />";  
//echo $_POST['list'];
   }
?>

 

but it shows me nothing. did I make any mistake?

Link to comment
Share on other sites

Yeah, you have a lot of syntactical errors, and since you don't have them turned on it just shows a blank page.

 

If you put this code at the top of your page it will display the errors that you have:

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

Like unska said, here is the solution, they HAVE TO choose an option from the drop down:

 

   if(isset($_POST['submit'])) {
      echo $_POST['list'];
   }
?>





</pre>
<form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="post">

car
bus
motorbike




</form>
<b

Link to comment
Share on other sites

Yeah, you have a lot of syntactical errors, and since you don't have them turned on it just shows a blank page.

 

If you put this code at the top of your page it will display the errors that you have:

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

Like unska said, here is the solution, they HAVE TO choose an option from the drop down:

 

<?php
   if(isset($_POST['submit'])) {
      echo $_POST['list'];
   }
?>

<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="list">
<option value="car">car</option>
<option value="bus">bus</option>
<option value="motorbike">motorbike</option>
</select>
<br>
<input type="submit" name="submit" value="Submit!">
</form>
</body>
</html>

 

if you don't select any thing from the list, why it is not showing any message, like "please choose one from the list"

Link to comment
Share on other sites

Because it was not specified. If you want it that way... modify a bit of the code with this:

 

   // replace all the code within if() bracket with this
   if(isset($_POST['submit'])) {
      if(empty($_POST['list'])) echo "please choose one from the list";
      else echo $_POST['list'];
   }

Link to comment
Share on other sites

if you don't select any thing from the list, why it is not showing any message, like "please choose one from the list"

 

Maybe I'm confused but with a SELECT, the first option is automatically selected by default, so it's impossible for someone not to choose an option.  Am I missing something...?

Link to comment
Share on other sites

I have tried with what Maq said and it did... it should have a default one right away even without specifying something. My previous post should be a bit nullified but might want to try it if it works for you.

 

By the way, have you tried clicking the submit button? It should work that way.

Link to comment
Share on other sites

if you don't select any thing from the list, why it is not showing any message, like "please choose one from the list"

 

Maybe I'm confused but with a SELECT, the first option is automatically selected by default, so it's impossible for someone not to choose an option.  Am I missing something...?

 

yes.....

this is the code for select

<select name="list">
<option></option>
<option value="car">car</option>
<option value="bus">bus</option>
<option value="motorbike">motorbike</option>
</select>

 

so 1st option is not selected.

its still not working >:( >:(>:(

Link to comment
Share on other sites

if you don't select any thing from the list, why it is not showing any message, like "please choose one from the list"

 

Maybe I'm confused but with a SELECT, the first option is automatically selected by default, so it's impossible for someone not to choose an option.  Am I missing something...?

 

yes.....

this is the code for select

<select name="list">
<option></option>
<option value="car">car</option>
<option value="bus">bus</option>
<option value="motorbike">motorbike</option>
</select>

 

so 1st option is not selected.

its still not working >:( >:(>:(

 

when I tried with this code, it shows me "The page cannot be found" error >:(:(

Link to comment
Share on other sites

Why do you have an empty option in your select field?

 

if I don't put an empty option, & the user don't change it, that means the user choose the wrong option.

thats why I want to make sure the user choose the right one and if it is blank, it should says "you have to choose one option from the list"

Link to comment
Share on other sites

Why do you have an empty option in your select field?

 

if I don't put an empty option, & the user don't change it, that means the user choose the wrong option.

thats why I want to make sure the user choose the right one and if it is blank, it should says "you have to choose one option from the list"

 

 

OK, doesn't make any sense to me but here:

 

   if(isset($_POST['submit'])) {
      if($_POST['list'] == "no")
  {   
      echo "You didn't choose an option.";
  } else { 
         echo $_POST['list'];
      }  
   }
?>





</pre>
<form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="post">


car
bus
motorbike




</form>
<b

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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