1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Form

Discussion in 'PHP' started by ssimon171078, May 4, 2015.

  1. #1
    i wrote form in php i need when i choose county ,script prints its capital of county can you help me?
    my code is :
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>PHP form select box example</title>
    <!-- define some style elements-->
    <style>
    label,a
    {
        font-family : Arial, Helvetica, sans-serif;
        font-size : 12px;
    }
    
    </style>   
    </head>
    
    <body>
    <?php
        $Capital=array("Washington","London","Paris","Mexico","Moscow","Tokyo");
        echo "now printing capitals\n";
       
        if(isset($_POST['formSubmit']))
        {
            $aCountries = $_POST['formCountries'];
           
            if(!isset($aCountries))
            {
                echo("<p>You didn't select any countries!</p>\n");
            }
            else
            {
                $nCountries = count($aCountries);
               
                echo("<p>You selected $nCountries countries: ");
                for($i=0; $i < $nCountries; $i++)
                {
                    echo($aCountries[$i] . " ");
                }
                echo("</p>");
            }
        }
    ?>
    
    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
        <label for='formCountries[]'>Select the countries that you have visited:</label><br>
        <select multiple="multiple" name="formCountries[]">
            <option value="US">United States</option>
            <option value="UK">United Kingdom</option>
            <option value="France">France</option>
            <option value="Mexico">Mexico</option>
            <option value="Russia">Russia</option>
            <option value="Japan">Japan</option>
        </select><br>
        <input type="submit" name="formSubmit" value="Submit" >
    </form>
    
    </body>
    </html>
    PHP:

    SEMrush
     
    ssimon171078, May 4, 2015 IP
    SEMrush
  2. Anveto

    Anveto Well-Known Member

    Messages:
    683
    Likes Received:
    36
    Best Answers:
    17
    Trophy Points:
    145
    #2
    What error are you getting? You could use
    print_r($_POST['formCountries'])
    PHP:
    to see what your array looks like when you submit. Most likely you could also replace action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" with action=""

    Have you tried name="formCountries" ?
     
    Anveto, May 4, 2015 IP
  3. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Not sure what the error is you have but I believe below might cause issues
    
    $aCountries = $_POST['formCountries'];
    
    if(!isset($aCountries))
    {
    Code (markup):
    the $aCountries = $_POST['formCountries']; will throw a warning when no countries are selected and possibly the 'isset' part will always return true, as it seems to me $aCountries is always set to something (blank or null, not sure) ...

    it only returns an array of values when at least 1 value is selected...
    try
    if(!isset($_POST['formCountries']))
    Code (markup):
    or
    if(is_empty($aCountries))
    Code (markup):

    edit : I think I understand what you mean...
    Build up your array as
    
    $Capital=array("united states"=>"Washington","united kingdom"=> "London","France"=>"Paris",..and so on...);
    Code (markup):
    allowing you to compare & link the city to the coutries based on the values you retrieve from the $_POST.. (keys are case sensitive) using in_array
    http://php.net/manual/en/function.in-array.php
     
    Last edited: May 4, 2015
    freelanceDeveloper, May 4, 2015 IP
  4. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Correction
    Use the values from your form's picklist , US, UK and not the descriptive words United states...
    
    $Capital=array("US"=>"Washington","UK"=> "London","France"=>"Paris",..and so on...);
    Code (markup):
    depending on the requirements you could use a database... this will prevent you to rewrite your script every time you want to add a country... I would at least be consitent in your form and always use the countrycode as value... so FR instead of France... https://countrycode.org/

    Open php mode again in the form and loop your array again to print the picklist, no need to redo this manually and make mistakes by doing so
     
    Last edited: May 4, 2015
    freelanceDeveloper, May 4, 2015 IP
    Anveto likes this.