Killbert
Killbert

Reputation: 171

Translate array into HTML form then submit function

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!

Upvotes: 1

Views: 129

Answers (1)

Adlin Ling
Adlin Ling

Reputation: 382

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!";
}

?>

Upvotes: 1

Related Questions