0

I am working on a form to create an athlete forename and surname. This works as it should, populating the appropriate parts of the database.

I have now come to add a drop down box in which they will select the athlete's country. Unfortunately, I cannot get this to show up in the athletecountry field of the database. This is in the same table as forename and surname.

I would hugely appreciate any help.

<?php echo ($error != "") ? $error : ""; ?>
<form action="createathlete.php" method="post">
<br>
<br>
Athlete Forename: <input type="text" value="<?php echo $athleteforename; ?>" name="athleteforename" /><br/>
Athlete Surname: <input type="text" value="<?php echo $athletesurname; ?>" name="athletesurname" /><br/>
Representing:   Country:     <select name=$athletecountry tabindex="1">
                 <optgroup label="Continent">
                    <option value="Country 1">Country 1</option>
                    <option value="Country 2">Country 2</option>
                    <option value="Country 3">Country 3</option>
                 </optgroup>
            </select>
<input type="submit" value="Register" name="submit-form" />
    </form>

Earlier in the page I also have this code which I cobbled together from a couple of other tutorials.

//initialize php variables used in the form
$athleteforename = "";
$athletesurname = "";
$userID = "";
$athletecountry = "";

//check to see that the form has been submitted
if(isset($_POST['submit-form'])) { 

//retrieve the $_POST variables
$athleteforename = $_POST['athleteforename'];
$athletesurname = $_POST['athletesurname'];
$athletecountry = $_POST['athletecountry'];

//initialize variables for form validation
$success = true;
$userTools = new UserTools();

//prep the data for saving in a new user object
    $data['athleteforename'] = $athleteforename;
    $data['athletesurname'] = $athletesurname;
    $data['athletecountry'] = $athletecountry;
    $data['userID'] = $user->id;


    //create the new user object
    $newAthlete = new Athlete($data);

    //save the new user to the database
    $newAthlete->save(true);
1
  • It was probably a bad idea to not put the whole page code here. pastebin.com/TvbxFAhK Commented Aug 28, 2015 at 10:42

3 Answers 3

1

It seems you are not giving proper name to select i.e $athletecountry

<select name=$athletecountry tabindex="1">

change to

<select name="athletecountry" tabindex="1">
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. Unfortunately, this has not fixed the problem. The athletecountry field is still not being updated in the database.
0

As per @Chandu and @taxicala answers, once the code is showing as "athletecountry" that should be fine.

Check that the Athlete() class is expecting all the elements in the array, perhaps the country string is being dropped because it's unexpected for some reason?

1 Comment

This was it - I had forgotten to tell the athlete class to handle the data. Instead I was expecting it, somehow, to magically head into the database. Thank you, internet stranger!
0

You have a typo in the name attr of the select:

Change:

<select name=$athletecountry tabindex="1">

To:

<select name="athletecountry" tabindex="1">

1 Comment

Thank you for your help. Unfortunately, this has not fixed the problem. The athletecountry field is still not being updated in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.