0

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, starURL: $('#starurl').val()}, 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';
?>


UPDATE ON STATUS: It nearly works just this part is leaving starURL value blank {starID:id, starURL: $('#starurl').val()} on the firebug console under post it shows this: starID=307&starURL=

4

1 回答 1

1

I don't know the details of your database, but a couple of things could be going wrong. Your SQL in response6.php has a syntax error and should look like:

$sql = "update stars set approved = 1, starURL = ? where starID = ?";

(i.e. separate with a comma instead of an ampersand)

Also, you are not passing a value for starURL in your ajax post call so it will be null when you bind it and execute the UPDATE statement. Did you want to include a value for starURL in your ajax post data, e.g.

$.post('response6.php', {starID:id, starURL: 'whatever'}, ...

Finally, doing a full refresh of your page is probably overkill. You are using ajax to post a lightweight request and could just do a partial update of your page when the response comes back.

于 2013-09-09T23:54:32.170 回答