Monday, 9 September 2013

Update a database field with value from a textbox using ajax and a button

Update a database field with value from a textbox using ajax and a button

I'm trying to show a field from the database, where the database field
starts off as saying textbox, like this:
307 , o = o ~ ongoing ~ (textbox) [approve button]
and I want to be able to be change the value in the textbox to say lala
and then press the approve button and have it update the field with the
new value in the database and with the window.location.reload(); it would
show the updated field value in the textbox so it ends up like this:
307 , o = o ~ ongoing ~ (lala) [approve button]

Here's the code from views.php:
echo "<br><b>request to join site: {$row9['count']}</b><br>";
while ($row99 = $prep99->fetch(PDO::FETCH_ASSOC)) {
echo "{$row99['starID']} , {$row99['starName']} = {$row99['twitter']}";
if ($row99['guest'] == 0) {
$guest = 'ongoing';
};
if ($row99['guest'] == 1) {
$guest = 'guest';
};
if ($row99['guest'] == 2) {
$guest = 'PLEASE SELECT ONE';
};
echo " ~ $guest ~ <br>";
echo "<input type='text' name='starurl' value='{$row99['starURL']}'
id='starurl' style='width:100px; height:30px;'/>";
echo "<br><button
onclick='save_a9({$row99['starID']})'>Approve</button><button
onclick='save_d9({$row99['starID']})'>Disapprove</button><br>";
}
Here's the function from the top of views.php:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
function save_a9(id) {
$.post('response6.php', {starID:id}, function(result) {
alert(result);
window.location.reload();
});
}
</script>
And here's the response6.php:
<?php
include("db_conn.php");
$sql = "update stars set approved = 1 & starURL = ? where starID = ?";
$qc = $pdo_conn->prepare($sql);
$qc->execute(array($_POST['starURL'], $_POST['starID']));
echo 'saved';
?>

No comments:

Post a Comment