Cheops Posted December 12, 2016 Share Posted December 12, 2016 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 constantsdefine ("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 connectionif ($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. Quote Link to comment https://forums.phpfreaks.com/topic/302720-html-form-and-php-script/ Share on other sites More sharing options...
requinix Posted December 12, 2016 Share Posted December 12, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302720-html-form-and-php-script/#findComment-1540234 Share on other sites More sharing options...
Cheops Posted December 12, 2016 Author Share Posted December 12, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302720-html-form-and-php-script/#findComment-1540238 Share on other sites More sharing options...
Barand Posted December 12, 2016 Share Posted December 12, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302720-html-form-and-php-script/#findComment-1540245 Share on other sites More sharing options...
Cheops Posted December 12, 2016 Author Share Posted December 12, 2016 (edited) 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 December 12, 2016 by Cheops Quote Link to comment https://forums.phpfreaks.com/topic/302720-html-form-and-php-script/#findComment-1540246 Share on other sites More sharing options...
Stielle Posted January 6, 2017 Share Posted January 6, 2017 Where exactly do I place this code I have a form that uses this specific code. It is placed at the top of my main php file. Quote Link to comment https://forums.phpfreaks.com/topic/302720-html-form-and-php-script/#findComment-1541104 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.