I have a page where user can add the records to the mysql database. On the same page there is a list existing records in the database.
I use ajax to add the record to database, sorry for old version of mysql, will get updated after I get this thing to work first:
$(document).ready(function(){
//on the click of the submit button
$("#btn_submit").click(function(){
//get the form values
var username = $('#hdn_username').val();
var name = $('#txt_name').val();
var brand = $('#txt_brand').val();
//make the postdata
var postData = 'username='+username+'&name='+name+'&brand='+brand;
//call your input.php script in the background, when it returns it will call the success function if the request was successful or the error one if there was an issue (like a 404, 500 or any other error status)
$.ajax({
url : "input.php",
type: "POST",
data : postData,
success: function(data,status, xhr)
{
//if success then just output the text to the status div then clear the form inputs to prepare for new data
$("#status_text").html(data);
$('#name').val('');
$('#brand').val('');
},
error: function (jqXHR, status, errorThrown)
{
//if fail show error and server status
$("#status_text").html('there was an error ' + errorThrown + ' with status ' + textStatus);
}
});
}); });
My html is:
<table class="simplet" width="640" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th width="300" colspan="2">Product Name</th>
<th width="150">Brand</th>
<th width="100">Quantity</th>
<th width="60">Price</th>
<th width="30">Delete</th>
</tr>
</thead>
<tbody>
<tr><td colspan="6"><div id="status_text_list" /></td></tr>
[insert_php]
$current_user= wp_get_current_user();
$username= $current_user->user_login;
mysql_connect("localhost","xxxxx","xxxx");//database connection
mysql_select_db("xxxx");
$query= "SELECT * FROM wp_userdata WHERE username='$username'";
$result= mysql_query($query);
if (!$result) { die("Error db request: <br />". mysql_error()); }
while ($result_row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo '<tr class="odd-row"><td width="30"></td><td>'.$result_row['product_name'].'</td><td>'.$result_row['product_brand'].'</td><td>'.$result_row['product_quantity'].'</td><td>'.$result_row['product_link'].'</td><td><a class="link-delete" href="#" data-delete-id="' . $result_row['id'] . '">X</a></td></tr>';
}
[/insert_php]
</tbody></table>
<br />
<table border="1">
<tr>
<td align="center">Add Products</td>
</tr>
<tr>
<td>
<form onsubmit="return false">
<table>
<input type="hidden" id="hdn_username" name="username" value="[insert_php]echo $username;[/insert_php]">
<tr>
<td>Product Name</td>
<td><input type="text" id="txt_name" name="name" size="50">
</td>
</tr>
<tr>
<td>Brand</td>
<td><input type="text" id="txt_brand" name="brand" size="50">
</td>
</tr>
<tr>
<td></td>
<td><div id="status_text" /></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" id="btn_submit" name="submit" value="Submit"></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
And my input.php is:
mysql_connect("localhost","xxxx","xxxx");//database connection
mysql_select_db("xxxx");
//inserting data order
$order = "INSERT INTO wp_userdata
(username, product_name, product_brand)
VALUES
('$_POST[username]',
'$_POST[name]',
'$_POST[brand]')";
//declare in the order variable
$result = mysql_query($order); //order executes
if($result){
echo ("DATA SAVED SUCCESSFULLY");
} else{
echo("<br>Input data is fail");
}
When I click the Submit button, form values sent to input.php and the success message is displayed, you can see it here - http://suppliesprices.com/?page_id=4
What I want to achieve is to add the new data row to the displayed list without reloading the page.
I was thinking to render it with javascript and add to a success ajax call, but not sure if it going to work.
How do I change my code to get the desired effect?