Jump to content

[SOLVED] help with form script please...


meomike2000

Recommended Posts

i have created a sample script with drop down list in my form. all works well exept that i want to be able to make the selection from the first drop down list affect the choices of the second drop down list. here is what i have so far.

 

<html>

<head></head>

<body>

<?php

Function DisplayForm($errors = null){

$error_string = $errors; // you could use $error_string = implode ("<br />",$errors); if you want to save performance by using an array insead.

echo('

'.$error_string.'<br />

<form method="POST" action="'.$_SERVER['PHP_SELF'].'">

Select from test1:

<select name="state">

<option value="test1.1">test1.1</option>

<option value="test1.2">test1.2</option>

<option value="test1.3">test1.3</option>

</select><br />

<br />');

// assign value $state = <select name="state">

this is were i seem to have the problem, getting the value from first drop down list into a string

 

$state =//not sure what to do here

 

switch ($state)

{

case 'test1.1':

echo ('

Select from test2:

<select name="city">

<option value="test2.1.1">test2.1.1</option>

<option value="test2.2.1">test2.2.1</option>

<option value="test2.3.1">test2.3.1</option>

</select><br />

<br /> ');

break;

case 'test1.2':

echo ('

Select from test2:

<select name="city">

<option value="test2.1.2">test2.1.2</option>

<option value="test2.2.2">test2.2.2</option>

<option value="test2.3.2">test2.3.2</option>

</select><br />

<br /> ');

break;

case 'test1.3':

echo ('

Select from test2:

<select name="city">

<option value="test2.1.3">test2.1.3</option>

<option value="test2.2.3">test2.2.3</option>

<option value="test2.3.3">test2.3.3</option>

</select><br />

<br /> ');

break;

default:

echo ('

Select from test2:

<select name="city">

<option value=" "> </option>

</select><br />

<br /> ');

break;

}

echo ('

Select from test3:

<select name="cat">

<option value="test3.1">test3.1</option>

<option value="test3.2">test3.2</option>

<option value="test3.3">test3.3</option>

</select><br />

Please enter a number between 1 and 10:

<input type="text" name="num" size="2">

<br />

<input type="submit" name="submit" value="Select">

</form>

');

}

//check if form has been submitted

if (!$_POST['submit'])

{

//if not display form

DisplayForm();

}

else

{

//form has been submitted

//items selected, check that number was entered,

$error = null; //Declare it so we dont get notices 'undeclared variable'

 

//check number range

if (!isset($_POST['num']) || !is_numeric($_POST['num']))

{

$error .='ERROR: Please enter a number between 1 and 10';

}

elseif ($_POST['num'] < 1 || $_POST['num'] > 10)

{

$error .='ERROR: The number must be between 1 and 10';

}

//if there was an error, show it.

if ($error != null){

DisplayForm($error);

}

else

{

//otherwise carry on?

$state = $_POST['state'];

$city = $_POST['city'];

$cat = $_POST['cat'];

echo 'Here is your selections: <br />';

echo "<i>$state</i><br />";

echo "<i>$city</i><br />";

echo "<i>$cat</i><br />";

}

}

?>

 

</body>

</html>

 

i am still fairly new at php... and any help with this would be great

i tried to put submit buttons in the first list but they do not show up in the list, they always show up net to the list. but that seems to work, problem is in a real script i want to be able to select a state and then have a selection of cities for that state show up in the second choice box..... hope somebody can understand what i am trying to ask....

 

thanks mike....

Link to comment
Share on other sites

for reading ease...

<html>
<head></head>
<body>
<?php
Function DisplayForm($errors = null){
   $error_string = $errors; // you could use $error_string = implode ("<br />",$errors); if you want to save performance by using an array insead.
      echo('
         '.$error_string.'<br />
         <form method="POST" action="'.$_SERVER['PHP_SELF'].'">
      Select from test1:
         <select name="state">
            <option value="test1.1">test1.1</option>
            <option value="test1.2">test1.2</option>
            <option value="test1.3">test1.3</option>
         </select><br />
         <br />');
      // assign value $state = <select name="state">
this is were i seem to have the problem, getting the value from first drop down list into a string

      $state =//not sure what to do here

      switch ($state)   
         {
            case 'test1.1':
            echo ('
                  Select from test2:
                     <select name="city">
                        <option value="test2.1.1">test2.1.1</option>
                        <option value="test2.2.1">test2.2.1</option>
                        <option value="test2.3.1">test2.3.1</option>
                     </select><br />
                  <br /> ');
                break;
            case 'test1.2':
            echo ('
                  Select from test2:
                     <select name="city">
                        <option value="test2.1.2">test2.1.2</option>
                        <option value="test2.2.2">test2.2.2</option>
                        <option value="test2.3.2">test2.3.2</option>
                     </select><br />
                  <br /> ');
                   break;
            case 'test1.3':
            echo ('
                  Select from test2:
                     <select name="city">
                        <option value="test2.1.3">test2.1.3</option>
                        <option value="test2.2.3">test2.2.3</option>
                        <option value="test2.3.3">test2.3.3</option>
                     </select><br />
                  <br /> ');
                   break;
            default:
            echo ('
                  Select from test2:
                     <select name="city">
                        <option value=" "> </option>
                     </select><br />
                  <br /> ');
                  break;
         }
   echo ('
      Select from test3:
         <select name="cat">
            <option value="test3.1">test3.1</option>
            <option value="test3.2">test3.2</option>
            <option value="test3.3">test3.3</option>
         </select><br />
      Please enter a number between 1 and 10:
         <input type="text" name="num" size="2">
         <br />         
      <input type="submit" name="submit" value="Select">
      </form>
   ');
}
//check if form has been submitted
if (!$_POST['submit'])
{
   //if not display form
   DisplayForm();
}
else
{
   //form has been submitted
   //items selected, check that number was entered,
   $error = null; //Declare it so we dont get notices 'undeclared variable'
   
   //check number range
   if (!isset($_POST['num']) || !is_numeric($_POST['num']))
      {   
         $error .='ERROR: Please enter a number between 1 and 10';
      }
   elseif ($_POST['num'] < 1 || $_POST['num'] > 10)
      {
         $error .='ERROR: The number must be between 1 and 10';
      }
   //if there was an error, show it.
   if ($error != null){
      DisplayForm($error);
      }
      else
      {
      //otherwise carry on?
   $state = $_POST['state'];
   $city = $_POST['city'];
   $cat = $_POST['cat'];
      echo 'Here is your selections: <br />';
      echo "<i>$state</i><br />";
      echo "<i>$city</i><br />";
      echo "<i>$cat</i><br />";
      }
}
?>

</body>
</html>

Link to comment
Share on other sites

Thanks lodius.

 

You misunderstand the way php and html/javascript work;

 

PHP is executed on the server, after the script has finished executing it send the result to the clients browser in the form of HTML and Javascript, only then the browser will display the html/javascript.

 

You cannot use php after html, only html after php.

 

You must either use a seperate page, or use AJAX, which is a form of javascript.

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.