1

I am a bit at lost as my PHP knowledge is very basic to say the least, but I am learning on the fly.

In a Wordpress plugin, I have the following php function:

$pool->get_leagues( true );

which gives a an array of league values: the id number and the name of the league.

Then there is this function:

$pool = new Football_Pool_Pool;
$pool->update_league_for_user( get_current_user_id(), <<THIS IS WHERE SELECTED ID NUMBER GOES>> );

I need to create an HTML form that lists the available league names that the user on a page can select in either an dropdown form, with radio buttons or plain links, whatever is easiest for the example.

Then, when the user makes a choice and submits the values, the league value should be updated into the database as per the above function.

Here are my total newbe / dummy questions:

  1. How does the PHP look that would create the desired action? where would I put this code? Do I create a whole new PHP page to handle this form, or do I need to enter it into one of the existing php pages somewhere?
  2. Based on answer 1, how does the HTML look that would display the form and call the php once submitted?
  3. If this is easier with javascript, please feel free to share that example.

Help is much much appreciated!

1 Answer 1

1

I think you have to create a whole new PHP file. Here the PHP code and the HTML are in a single PHP file.

<?php


if(!isset($_POST['submit'])){

  //if the form has not been submitted yet, display the form
  echo "<form name='myform' action='' method='POST'>";
  //Get array of leagues
  $leagues = $pool->get_leagues(true);

  //Make a drop down
  echo "<select name='league'>";

    foreach($leagues as $league){
        echo "<option>$league</option>";
    }
  echo "</select>";

  echo "<input type='submit' name='submit' value='Submit'>";
  echo "</form>";

}else{

//If the form has been submitted, run the PHP function to update database
  $pool = new Football_Pool_Pool;
  $pool->update_league_for_user(get_current_user_id(), $_POST['league']);
  echo "Database updated!";
}

?>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this is getting me somewhere. But not all the way just yet. I call this php page from another page what has just executed some predictions the user enters for football matches. The bottom of that form now displays a link "click here to register for a league" and that links to the above php page. When clicked I get an empty page. The source code of that page displays just this:'<form name='myform' action='' method='POST'>' So it seems the php script is not getting the data as requested...
Could you please clarify? I'm not sure if I understand what you are trying to accomplish. If all you have is a link to the above PHP page and you click on that link, you will get the form just like as if you type the url to the page. The script will not get any data until you click on the submit button on the form.
Nevermind, I got it to work with your example, just had to change the array. Thanks for your help!!!
You're welcome. Please vote up my answer. I would really appreciated it.

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.