Jump to content

Recommended Posts

I would like to link my HTML form and PHP script such that when a user passes values via the form they are processed by the PHP script and a relevant response given. My PHP script is as follows;

 

 

<?php
//create server and database connection constants
define ("DB_SERVER", "localhost");
define ("DB_USER","root");
define ("DB_PASSWORD","");
define ("DB_NAME", "project");
global $con;
$con = new mysqli (DB_SERVER,DB_USER,DB_PASSWORD, DB_NAME);

//Check server connection
if ($con->connect_error){
    die ("Connection failed:". $con->connect_error);
}else {
    echo "Connected successfully <br />";
}
// Calculating the attack stength
    function predictor ($home_team,$away_team){
        global $con;
        $result=[0,0];
        
        
        // For the  home team
        $home_team_strength=get_query_value($con,"SELECT SUM(HTgoals)/Count(*) FROM `results` WHERE Home_team = '$home_team' ");

        $league_strength=get_query_value($con,"SELECT SUM(HTgoals)/Count(*) FROM `results`");

        $home_team_attack_strength=$home_team_strength/$league_strength;  

        $away_team_strength = get_query_value($con, "SELECT SUM(HTgoals)/Count(*) FROM `results` WHERE Away_team='$away_team'");
        
        $away_team_defence_strength=$away_team_strength/$league_strength;
        
        $result[0]=$home_team_attack_strength*$away_team_defence_strength*$league_strength;
        
        // For the away team
        
        $away_team_strength_2 = get_query_value($con, "SELECT SUM(ATgoals)/Count(*) FROM `results` WHERE Away_team='$away_team'");

        $league_away_strength=get_query_value($con, "SELECT SUM(ATgoals)/Count(*) FROM `results`");
        
        $away_team_attack_strength =$away_team_strength_2/$league_away_strength;
        
        $home_team_defence =get_query_value($con, "SELECT SUM(ATgoals)/Count(*) FROM `results` WHERE Home_team='$home_team'");
        
        $home_team_defence_strength =$home_team_defence/$league_away_strength;

        $result[1] = $away_team_attack_strength*$home_team_defence_strength*$league_away_strength;
        
        return $result;
    }

    
    
    
        function get_query_value ($con,$query){
        $return_value=-1;
        
        $con->real_query($query);
        $result=$con->use_result();
        $row=$result->fetch_row();
        $result->close();
        return $row[0];
        
    }

 

 My HTML form is below;

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Prediction</title>
            <form>
                <form action="predictor.php" method="POST">
                <font color ="blue">                
                <div align="center">
                <p> Please enter the teams you would like to view predictions for</p>
                <p> Enter the home team</p>
                Home_team:<select name = "Home_team"> <br>
            
                
                <option value = "Bandari">Bandari</option>
                <option value = "Chemelil">Chemelil</option>
                <option value = "Gor Mahia">Gor Mahia</option>
                <option value = "Kakamega Homeboyz FC" >Kakamega Homeboyz FC</option>
                <option value = "Leopards">Leopards</option>
                <option value = "Mathare Utd">Mathare Utd</option>
                <option value = "Muhoroni">Muhoroni</option>
                <option value = "Nairobi City">Nairobi City</option>
                <option value = "Rangers">Rangers</option>
                <option value = "Sony Sugar">Sony Sugar</option>
                <option value = "Sofapaka">Sofapaka</option>
                <option value = "Thika Utd">Thika Utd</option>
                <option value = "Tusker">Tusker</option>
                <option value = "Ulinzi Stars">Ulinzi Stars</option>
                <option value = "Ushuru">Ushuru</option>
                <option value = "Western Stima">Western Stima</option>
            </select><br><br>
                
                <p> Enter the away team</p>
                Away_team:<select name = "Away_team"><br>
                    
                
                <option value = "Bandari">Bandari</option>
                <option value = "Chemelil">Chemelil</option>
                <option value = "Gor Mahia">Gor Mahia</option>
                <option value = "Kakamega Homeboyz FC" >Kakamega Homeboyz FC</option>
                <option value = "Leopards">Leopards</option>
                <option value = "Mathare Utd">Mathare Utd</option>
                <option value = "Muhoroni">Muhoroni</option>
                <option value = "Nairobi City">Nairobi City</option>
                <option value = "Rangers">Rangers</option>
                <option value = "Sony Sugar">Sony Sugar</option>
                <option value = "Sofapaka">Sofapaka</option>
                <option value = "Thika Utd">Thika Utd</option>
                <option value = "Tusker">Tusker</option>
                <option value = "Ulinzi Stars">Ulinzi Stars</option>
                <option value = "Ushuru">Ushuru</option>
                <option value = "Western Stima">Western Stima</option>
                <select><br><br>
                <td>
                
                <input type="submit" name="submit" value="Submit"/>
            </form>

  

Any ideas it is currently not working.

 

Link to comment
https://forums.phpfreaks.com/topic/302720-html-form-and-php-script/
Share on other sites

Sounds like you're asking for plain ol' form-processing-in-PHP code. Since your form is already pointing to predictor.php, make the code in there look like

<?php

if (isset($_POST["submit"])) { // submit button was pressed
	/* process the form data however you want */
} else {
	/* form was not submitted. you can display the form here or redirect back to the form page or whatever. */
}
To get the values you need to pass to your predictor() function, use $_POST. The function will return a value that you can output however you want.

Sounds like you're asking for plain ol' form-processing-in-PHP code. Since your form is already pointing to predictor.php, make the code in there look like

<?php

if (isset($_POST["submit"])) { // submit button was pressed
	/* process the form data however you want */
} else {
	/* form was not submitted. you can display the form here or redirect back to the form page or whatever. */
}
To get the values you need to pass to your predictor() function, use $_POST. The function will return a value that you can output however you want.

 

Where exactly do I place this code

I see you are still adhering to the mantra "Why run one query when six will do?"

 

https://forums.phpfreaks.com/topic/302526-divide-by-zero-warning/?do=findComment&comment=1539284

Its badly written but I managed to get it working so I didnt check on how to reduce the number of queries to one though I understood what you were implying and it actually looked simpler and less lines of code

Edited by Cheops
  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.