Hello.

I am having a bit of trouble with being able to submit/record the data from a selected dropdown field to my DB.

Here is what I know so far:

  1. Connection to DB is fine; All other data fields are submitted and recorded to DB
  2. The dropdown (To select country of origin) works fine on the front-end
  3. Upon submission all other fields EXCEPT the dropdown field is dumped into the DB correctly.

I am missing something fundamental here.

Here is a snippet (Full code upon request)

function NewUser()
{  
    $userName = $_POST['user'];
    $email = $_POST['email'];
    $password =  $_POST['pass'];
    $country = $_POST['country'];
    $query = "INSERT INTO WebsiteUsers (user,email,pass,country) VALUES ('$userName','$email','$password', '$country')";
    $data = mysql_query ($query)or die(mysql_error());
    if($data)
    {
    echo "YOUR REGISTRATION IS COMPLETED...";
    }


    ...


    <label class="grey" for="dropdown">Country:</label>       
    <name="country" id="country" size="23" />

  <select name="countries" id="countries" style="width:290px;">
  <option value='us' data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag us" data-title="United States"      selected="selected">United States</option>
  <option value='ad' data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag ad" data-title="Andorra">Andorra</option>

I think it has something to do with this:

<label class="grey" for="dropdown">Country:</label>       
<name="country" id="country" size="23" />

I am a bit confused as to what I'm doing wrong - Probably something obvious that I'm missing.

Thank you in advance for any help!
Matthew

Hello mattyd,

Can you explain what this is representing and what it is used for? There is no HTML element called "name".

<name="country" id="country" size="23" />

I've gotten myself a bit mixed up, I fear.

I started off thinking I was working in JS, then said, "No, it's HTML."

Some more code (Showing other fields in form:

<!-- Register Form -->
                <form method="POST" action="dbConnect.php">
                    <h1>Not a member yet? Sign Up!</h1>

                    <!-- Username Field -->              
                    <label class="grey" for="signup">Username:</label>
                    <input class="field" type="text" name="user" id="user" value="" size="23" />

                    <!-- Email Field(s) -->  
                    <label class="grey" for="email">Email:</label>
                    <input class="field" type="text" name="email" id="email" size="23" />

                    <!-- Password Field -->
                    <label class="grey" for="email">Password:</label>
                    <input class="field" type="text" name="pass" id="pass" size="23" />



<!-- Country Dropdown -->

<label class="grey" for="dropdown">Country:</label>       
<name="country" id="country" size="23" />

<select name="countries" id="countries" style="width:290px;">
 <option value='us' data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag us" data-title="United States" selected="selected">United States</option>
  <option value='ad' data-image="images/msdropdown/icons/blank.gif" data-imagecss="flag ad" data-title="Andorra">Andorra</option>

Thank you!
Matthew

So the problem i see is that in line 6 of your code above, you are trying to recover the posted value for county. $country = $_POST['country'];

However, in your form, there should be an input element with a name="county". The only item i see that is fairly related is <name="country" id="country" size="23" />. However this is not an input element. its not an element at all.

Maybe you meant <input type="text" name="country" id="country" size="23" />.

However, that doesnt seem correct because I beleive that you are trying to get the value of the dropdown, not a value from some input field, correct?

Then I think what you want to change in your code is line 6 and delete line 19 (line number from your first post)

$country = $_POST['countries'];

commented: Always very helpful. +7

JorgeM:

Hi.

I made the changes as you suggested, above.

It is still not dumping any country information into the DB.

I am re-reading all my code and looking for errors.

Thank you for your assistance. You're always very helpful.

Matthew

I'll try to replicate this on my end. Will get back to you.

commented: Thank you, Sir! +0

Nothing I am trying is working.

Really lost on this one. (And, I know it's something simple)

Ok, so here is a working example. Its not complete with regard to your web application, but it does provide a basic form, submission, and displaying of results when the postback occurs. This process can be split across different pages by changing the action of the form, but I wanted to provide you at least a sample so you can see how the form data is collected, then passed via the post action, then on the page load, check to see if the form was submitted and if it was, display the form data.

<div style="border:1px solid #7f7f7f;background-color:#efefef;width:640px;padding:5px 25px 30px;margin-bottom:50px;">    
<form method="POST" action="index.php">
    <h2>Not a member yet? Sign Up!</h2>
    <table>
    <tr><td><label class="grey" for="user">Username:</label></td>
    <td><input class="field" type="text" name="user" id="user" /></td></tr>
    <tr><td><label class="grey" for="email">Email:</label></td>
    <td><input class="field" type="text" name="email" id="email" /></td></tr>
    <tr><td><label class="grey" for="pass">Password:</label></td>
    <td><input class="field" type="text" name="pass" id="pass" /></td></tr>
    <tr><td><label class="grey" for="dropdown">Country:</label></td>     
    <td><select name="country" id="countries" style="width:155px;">
        <option value='us' selected="selected">United States</option>
        <option value='ad'> Andorra</option>
    </select></td></tr>
    </table>
    <input type="Submit" value="Submit" name="submit" />
</form>
</div>

<?php
    if (isset($_POST['submit'])){
        $userName = $_POST['user'];
        $email = $_POST['email'];
        $password =  $_POST['pass'];
        $country = $_POST['country'];

        echo "<h2Results</h2>";
        echo "<table>";
        echo "<tr><td>Username<td><td>$userName</td></tr>";
        echo "<tr><td>Email<td><td>$email</td></tr>";
        echo "<tr><td>Password<td><td>$password</td></tr>";
        echo "<tr><td>Country Code<td><td>$country</td></tr>";
        echo "</table>";
    }
?>

Demo: http://itg.somee.com/dw/dw-472896/

Oh, and of course, once you have the form and the submission working as you expect, then you can move on to working on getting the data uploaded to your database. You can further develop lines 7-12 from your original sample code.

hope this helps.

Trying this now...

Will reply with results soon.

I tried it:

  1. The Demo link works
  2. I created a new page on my server with your PHP- That did not work - No data displayed on submission, sent me to a "page not found".

My problem is, no matter what I've tried in the last day, I cannot get any sort of country data added to the table; All other submission data is populating DB as expected - No problems.

Thanks,
Matthew

Does the submission of the demo echo the selected value for the country?
Also, have you double checked your mysql log to see what query is being run?

Yes, the demo echos the country code-ID.

In my version on my server, it does not.

I am using phpMyAdmin - Not sure where to check the logs using this.

Thanks,
Matthew

The Demo link works

the sample code i provided above should work for you too.

I created a new page on my server with your PHP- That did not work - No data displayed on submission, sent me to a "page not found".

If the page you created is something other than index.php, then you would have to update the form action attribute.

Ok, so here is my suggestion..I use this approach whenever i get stuck.

Keep what you have so far, but start a new page. On the new page, use the sample code i gave you. Get that to work first, then once that works, build up on it. Dont move foward until you have the basic page with the form and you can submit the form and retrieve the values.

The demo i provided you with online is as shown. I'm not doing any magic in the background so you probably have a simple mistake that's causing you this frustruation.

commented: Great advice! Will do. +0

Yes, Sir, I will try all your suggestion(s).

Thank you for all you time and assistance.

Matthew

After all this time struggling with this issue, a simple mistake.

I neglected to inspect my dbConnect.php file. Duh.

It lacked any form of info related to the countries.

I altered that file and then... It works!

Thank you all for everyone's patience with me and assistance.

Matthew

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.