.val()在单击按钮时不捕获文本框值

时间:2013-09-10 02:51:39

标签: php jquery ajax button sql-update

我正在尝试在文本框中输入一个值并让它触发数据库的更新语句,但帖子.val()返回一个空值。

这是功能:

<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>

这是按钮和文本框:

    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>";
}

这是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';
?>

1 个答案:

答案 0 :(得分:1)

我会改用属性id。例如

<button id="button-id">Click me</button>

并在jQuery中使用它。 Example of Javascript