Jump to content

please help with form, never used ajax, just heard it is what i need....


meomike2000

Recommended Posts

What I am trying to do is create a sample, or test, form.

I have created 2 sample forms with html/php as follows:

 

<html>
<head></head>
<body>
<?php

//display form to choose state
?>
<form method="POST" action="state.php">
    Choose a state:
        <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 />
    <input type="submit" name="submit" value="Select">
</form>
    
</body>
</html>

 

This form gives me a way to make my first selection......

and the second:

 

<html>

<head>
<title>state</title>
</head>

<body>

<?php
if (!$_POST['submitcity'])
{
    //the form has been submitted
    //look for and process 
    $state = $_POST['state'];
    //display state selected
    echo "$state <br />";
    //select which list to display next
?> 
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<?php
        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 />
                    <input type="hidden" name="state" value="test1.1" />
                        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 />
                    <br />
                 <input type="submit" name="submitcity" value="Select"> ');
                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 />
                    <input type="hidden" name="state" value="test1.2" />
                        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 />
                    <br />
                 <input type="submit" name="submitcity" value="Select"> ');
                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 />
                    <input type="hidden" name="state" value="test1.3" />
                        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 />
                    <br />
                 <input type="submit" name="submitcity" value="Select"> ');
                break;
        }
}
else
{
    //if "submit" variable exist
    //the form has been submitted
    //look for and process
    $state = $_POST['state']; 
    $city = $_POST['city'];
    $cat = $_POST['cat'];
    //display state selected before
    echo "$state <br />";
    //display city selected
    echo "$city <br />";
    //dislay the cat selected
    echo "$cat <br />";
}

?>

</body>
</html>

 

this second form script uses your choice from the first form to determine which set of choices to let you choose from....

 

I  would like to create a form that would let you choose a state from the first drop down menu, and that would determine the list of cities that would display in  the second drop down list.

 

hope somebody understands what I am trying to say and can help me or point me to a tutorial that can help me....

 

thanks mike....

Link to comment
Share on other sites

You could do this two ways, and AJAX would be the best. But, if you don't know the first thing about AJAX, you'll need to learn. If you don't want to, the other option would be to pre-build a JavaScript library with all the cities in it. Then run a function that checked the state and builds the city list.

 

Really, though, you should use AJAX. Here's a good tutorial to get you started:

http://www.w3schools.com/ajax/default.asp

Link to comment
Share on other sites

i have read the tutorial and one at tigzag.com that is very similar to the one at w3.

 

but nether of them give much detail on how to do what i want to do. can somebody give me a link to a tutorial that would show more on how to do what i am trying to do.

 

thanks mike......

Link to comment
Share on other sites

i have copy and pasted the sample form, php script, and the ajax.js files, i followed the instructions, the form comes up but doesnt function. what could be wrong......

 

i am trying to learn how to use ajax but can not figure this out.

 

here is the link: http://www.dhtmlgoodies.com/index.html?whichScript=ajax_chained_select

 

i am running ubuntu 8.04, have apache2, and php5 on machine. that all works as my other scripts work......

 

please help......

 

mike.......

Link to comment
Share on other sites

I think you start something like this

 

For the chained dropdown problem following files will be created:

 

1. html - design your form

 

2. javascript -create your xmlhttprequest

 

3. php file which will accept country query the database and return the states.

 

Now in which part you are getting stuck !! i think in javascript ??

 

Link to comment
Share on other sites

the problem was with the link to the js file. i fixed this link and now the sample is working and i can experiment with it to try to get it to do what i want.

 

original:

<script type="text/javascript" src="ajax.js"></script>

 

after fix:

<script type="text/javascript" src="js/ajax.js"></script>

 

this was just a copy past from the link above and i thought that i followed the instructions exactly???!!!! not sure.

 

thanks mike.......

Link to comment
Share on other sites

If you're using Firefox, go to Tools -> Error Console (or Ctrl+Shift+J). That will open your JavaScript error console. You'll probably see TONS of crap, so clear it. Then reload your page and run your stuff, all while watching the error console. If you don't understand the error, post the error here. If you don't see any errors, tell us and we'll go from there.

 

I think IE and others have similar tools, but I am unfamiliar with them.

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.