javascript - PHP noob having problems posting drop-down values into MySQL database. My table formatting is off too. Can anyone shed some light on this? -
building football prediction website. getting thee home_team names , away team names fixtures table in dbms, corresponding drop down boxes each fixture user can predict score. cant work. grateful help!
//establish connection <?php $connection = mysql_connect('localhost', 'root', 'password'); mysql_select_db('mls'); $query = "select * fixtures fixture_id between '1' , '10' "; $result = mysql_query($query); $num = mysql_num_rows($result); if($num>0){ echo"<table>"; echo "<th>home team</th>"; echo "<th>home score</th>"; echo "<th>away score</th>"; echo "<th>away team</th>"; for($count=0;$count<$num; $count++){ $row = mysql_fetch_array($result); echo"<tr> <td>".$row['home_team']."</td> <td> <form id="myform" method="post" action="process3.php"> <select name="home_score"> <select id='h".$count."'> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select > </td> <td> <form id="myform" method="post" action="process3.php"> <select name="home_score"> <select id='a".$count."'> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> </td> <td>".$row['away_team']."</td> </tr>"; } echo"</table>"; <input type="submit" title="submit form"> </form> } ?> <html> <?php //process3.php file <?php include_once('db.php'); $home_score = $_post['home_score']; $away_score = $_post['away_score']; if(mysql_query("insert user_prediction values('$home_score', '$away_score')")){ $result = "successfully inserted"; else $result = "insert failed"; ?> //myscript.js file ?> $("#sub").click(function(){ $.post( $("#myform).attr("action"), $("#myform:input").serializearray(), function (info){$("#result").html(info);}); }); $("#myform").submit(function(){ return false; });
while there plenty of things causing problems, notice here:
<select name="away_score"> //count id unique values in dropdown <select id='a".$count."'>
$count literally represented here string "$count", due code not being contained in php tags. try correcting instances of code similar below:
<?php echo '<select name="away_score">'; //count id unique values in dropdown echo '<select id="a' . $count . '">'; ?>
Comments
Post a Comment