I'm new to programming. I've become pretty good with HTML, CSS, and jQuery, but I may have accidentally gotten in too deep with this project. Before I go back and learn about server-side programming the proper way, I'm determined to get this page to work.
I have an HTML page set up that takes four inputs (firstName, lastName, grade, and PR) and I want these inputs to be saved into the table below (I'm using bootstrap, not sure if that affects anything). Here is the HTML:
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h1 style="text-align:center">1600M Run - PR Sheet</h1>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<form style="text-align: center;">
First name: <input type="text" id="firstName"></input>
Last name: <input type="text" id="lastName"></input>
Grade: <input type="text" id="grade"></input>
1600M PR: <input type="text" id="PR"></input>
<input type="submit" value="Enter" id="enterInfo"></input>
</form>
</div>
</div>
</div>
<table class="table">
<thead>
<tr>
<td><strong>First Name</strong></td>
<td><strong>Last Name</strong></td>
<td><strong>Grade</strong></td>
<td><strong>PR</strong></td>
</tr>
</thead>
<tbody id="runnerInfo">
<tr>
<td>Brady</td>
<td>Sheridan</td>
<td>12</td>
<td>4:42</td>
</tr>
</tbody>
</table>
I'm using jQuery+AJAX and PHP to send the input to a MySQL database called "runnerdata", then log the input to the table. Here is the javascript:
$(function(){
$('#enterInfo').click(function (){
var firstName = $('#firstName').val();
var lastName = $('#lastName').val();
var grade = $('#grade').val();
var PR = $('#PR').val();
$.ajax({
url: 'prentry.php',
type: 'post',
datatype: 'json',
data: {'firstName': firstName, 'lastName': lastName, 'grade': grade, 'PR': PR},
success: function(data){
var firstName = data[0];
var lastName = data[1];
var grade = data[2];
var PR = data[3];
$('#runnerInfo').append("<tr><td>"+firstName+"</td><td>"+lastName+"</td><td>"+grade+"</td><td>"+PR+"</td></tr>");
};
});
});
});
And here is the PHP:
<?php
//Connect to database
$host = "localhost";
$user = "root";
$pass = "root";
$databaseName = "prentry";
$tableName = "runnerdata";
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
//Retrieve input from jQuery
var $fname = $_POST['firstName']
var $lname = $_POST['lastName']
var $grade = $_POST['grade']
var $PR = $_POST['PR']
//Store input in database
$sql = "INSERT INTO runnerdata (firstName, lastName, grade, PR)
VALUES ($fname, $lname, $grade, $PR)";
//Return data
$result = mysql_query("SELECT * FROM $tableName");
$array = mysql_fetch_row($result);
echo json_encode($array);
?>
When I test the page, nothing happens. When I check my database, it hasn't been updated either. I have no doubt that I'm doing something horribly wrong, the server-side part of this project is just a conglomeration of like 6 different tutorials. The one thing I am sure of is that I have all the correct libraries linked in my <head></head> tags.
Any help is appreciated, I'd really like to move on from this project!
$sqlvariable with your mysql_query method. All you are doing is setting your insert query into a variable, and then never use it. Also since you are not escaping your values or putting them in quotes in the query, it will probably fail on a syntax error