0

I am trying to set the value to be displayed in a SELECT list using php scripts. What I have done is create an input html page (MatchSelect.php) which shows two select boxes and a submit button. On pressing the submit button a new new php file is called (MatchSelectResult.php) which is as follows;

 <!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>Seniors Inter-Club Match Management</title>

<link rel="stylesheet"  href="MainBody.css">
<link href="dropDown.css?v=1.1" rel="stylesheet" >
<?PHP require '../../configure.php';
include "Main_PHP_Code.php" ;
?>

</head>

<body>
<?PHP include "MatchPopulate.php"; ?>
<div class="container">
    <?PHP include "menu.txt" ?>
    <div class="content">  
         <div>
            <h1>Team Selection</h1>
            <form name="matchSelect" method="POST" action="MatchUpdate.php">

            <p>&nbsp;
            <select id = "Venue" name= "Venue" >
                <option disabled selected value> -- select an option -- </option>
                        <option value="Away">Away</option>
                <option value="Home">Home</option>
              </select>
            match against 
                    <select id ="Opponents" name ="Opponents">
                            <?php 
                                Global $OpponentName;
                                $oop = $OpponentName;
                                opponent_load('$oop');
                                ?>
                </select> 
etc.

the function opponent_load() is contained within the "Main_PHP_Code.php" code and is as follows;

function opponent_load($oppon){
Global $OpponentName;  
$db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS );
$database = "matchmanagementdb";
$db_found = mysqli_select_db($db_handle, $database);
if ($db_found) {
$SQL = "SELECT * FROM opponentsdb";
$result = mysqli_query($db_handle, $SQL);
while ( $db_field = mysqli_fetch_assoc($result) ) {

    $uName = $db_field['Opponents'];
    if ($uName == $oppon)
    {
        $selected = 'selected="selected"';
    }
    else
    {
        $selected = '';
    }
    echo "<option value='$uName' $selected> $uName </option>";
}
}
else {
    print "Database NOT Found ";
}
mysqli_close($db_handle);
}

The "MatchPopulate.php" code in the HEAD section is used to search the mySQL database using the two values from MatchSelect.php page. If the data is found, then the global variable $OpponentName is defined. The code is thus;

<?php 
Global $OpponentName;
//require '../../configure.php';
$uOpponentName = $_POST['Opponents'];
$uVenue = $_POST['Venue'];

//$db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS );
$database = "matchmanagementdb";
$conn = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
// check to see if Match (Opponents + Venue)already in the database, if so, retrieve data or add match to database
$SQL = "SELECT * FROM teamselect WHERE Opponents = '$uOpponentName' AND Venue = '$uVenue'";
$result = $conn->query($SQL);
//if $result->num_rows >0 then retrieve data  ELSE add match to database
if (!$result){
    print "Error selecting record: " .  $sql . "<br>" . $conn->error;
}   else {
        if ($result->num_rows >0) {
                while($row = $result->fetch_assoc()) {
                    $OpponentName = $row['Opponents'];
                } 
            } else { 
                $sql = "INSERT INTO teamselect (Opponents, Venue) VALUES ('$uOpponentName', '$uVenue')";
                if ($conn->query($sql) === TRUE) {
                } else {
                    print "Error adding record: " .  $sql . "<br>" . $conn->error;
                }
            }
         } 
$conn->close();
?>    

The code stops when it tries to populate the Opponents Select box on MatchSelectResult.php. Any help to solve this would be appreciated.

3
  • Can you please include the php error you are getting? Commented Feb 18, 2018 at 16:52
  • I am not getting an error message. The html page stops loading at the point when the page loads the Select box for opponents. Commented Feb 18, 2018 at 17:03
  • 2
    Turn on error_reporting and read error message. Commented Feb 18, 2018 at 17:10

1 Answer 1

1

I have solved the problem by opening a session and using $_SESSION["Opponents"] to pass the variable around the scripts. Also changed opponent_load('$oop') to opponent_load($oop).

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

Comments

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.