使用javascript更新文本框中的值

使用javascript更新文本框中的值,javascript,php,html,ajax,json,Javascript,Php,Html,Ajax,Json,事实上,我只是一个新手程序员,我为我的程序制作了自己的更新javascript函数,但遗憾的是代码没有更新..有人能帮我让代码工作吗?求你了 我想做的是,如果我更改文本框中的值并单击“更新”,它将更新 脚本: <script type="text/javascript"> $(document).ready(function() { $('#updates').click(function() { $id1 =

事实上,我只是一个新手程序员,我为我的程序制作了自己的更新javascript函数,但遗憾的是代码没有更新..有人能帮我让代码工作吗?求你了

我想做的是,如果我更改文本框中的值并单击“更新”,它将更新

脚本:

<script type="text/javascript">
        $(document).ready(function() {
            $('#updates').click(function() {
                $id1 = $('#id1').val();
                $di2 = $('#id2').val();
                $name1 = $('#name1').val();
                $name2 = $('#name2').val();
                $optA1 = $('#optA1').val();
                $optA2 = $('#optA2').val();
                $optB1 = $('#optB1').val();
                $optB2 = $('#optB2').val();
                $other_qual1 = $('#other_qual1').val();
                $other_qual2 = $('#other_qual2').val();
                $interview1 = $('#interview1').val();
                $interview2 = $('#interview2').val();
                $total1 = $('#total1').val();
                $total2 = $('#total2').val();

                $.ajax({
                    type: "POST",
                    cache: false,
                    url: "update.php",
                    data: "id1_text="+$name1,
                    data: "id2_text="+$name1,
                    data: "name1_text="+$name1,
                    data: "name2_text="+$name2,
                    data: "optA1_text="+$optA1,
                    data: "optA2_text="+$optA2,
                    data: "optB1_text="+$optB1,
                    data: "optB2_text="+$optB2,
                    data: "other_qual1_text="+$other_qual1,
                    data: "other_qual2_text="+$other_qual2,
                    data: "interview1_text="+$interview1,
                    data: "interview2_text="+$interview2,
                    data: "total1_text="+$total1,
                    data: "total2_text="+$total2,
                    success: function(data) {
                        alert('data has been updated!');
                    }
                });
            });
        });
    </script>

$(文档).ready(函数(){
$(“#更新”)。单击(函数(){
$id1=$('#id1').val();
$di2=$('#id2').val();
$name1=$('#name1').val();
$name2=$('#name2').val();
$optA1=$('#optA1').val();
$optA2=$('#optA2').val();
$optB1=$('#optB1').val();
$optB2=$('#optB2').val();
$other_qual1=$('other_qual1').val();
$other_qual2=$('other_qual2').val();
$interview1=$('#interview1').val();
$interview2=$('#interview2').val();
$total1=$('#total1').val();
$total2=$('#total2').val();
$.ajax({
类型:“POST”,
cache:false,
url:“update.php”,
数据:“id1_text=“+$name1,
数据:“id2_text=“+$name1,
数据:“name1_text=“+$name1,
数据:“name2_text=“+$name2,
数据:“optA1_text=“+$optA1,
数据:“optA2_text=“+$optA2,
数据:“optB1_text=“+$optB1,
数据:“optB2_text=“+$optB2,
数据:“other_qual1_text=“+$other_qual1,
数据:“other_qual2_text=“+$other_qual2,
数据:“interview1_text=“+$interview1,
数据:“interview2_text=“+$interview2,
数据:“total1_text=“+$total1,
数据:“total2_text=“+$total2,
成功:功能(数据){
警报('数据已更新!');
}
});
});
});
update.php代码:

<?php
mysql_connect("localhost","root","") or die ("cant connect to database!");
mysql_select_db("test") or die ("cant find database!");
$id1=$_POST['id1_text'];
$id2=$_POST['id2_text'];
$name1=$_POST['name1_text'];
$name2=$_POST['name2_text'];
$optA1=$_POST['optA1_text'];
$optA2=$_POST['optA2_text'];
$optB1=$_POST['optB1_text'];
$optB2=$_POST['optB2_text'];
$other_qual1=$_POST['other_qual1_text'];
$other_qual2=$_POST['other_qual2_text'];
$interview1=$_POST['interview1_text'];
$interview2=$_POST['interview2_text'];
$total1=$_POST['total1_text'];
$total2=$_POST['total2_text'];

$query1=mysql_query("UPDATE score SET name='$name1', score1='$optA1', score2='optB1', other_qual='$other_qual1', interview='$interview1', total='$total1' WHERE id='$id1'");
$resource1 = mysql_query($query1) 
or die (mysql_error());

$query2=mysql_query("UPDATE score SET name='$name2', score1='$optA2', score2='optB2', other_qual='$other_qual2', interview='$interview2', total='$total2' WHERE id='$id2'");
$resource2 = mysql_query($query2) 
or die (mysql_error());
?>

您的代码中有不少错误。例如,在JS代码中分配
数据时,语法错误。应采用以下形式:

data: { val:$("#selector").val, val2:$("#selector2").val(), /* etc etc*/ }
解决方案 请尝试以下代码:

JS

<script type="text/javascript">
$(document).ready(function () {
       $('#updates').click(function (e) {

           e.preventDefault();
           var id1 = $('#id1').val();
           var id2 = $('#id2').val();
           var name1 = $('#name1').val();
           var name2 = $('#name2').val();
           var optA1 = $('#optA1').val();
           var optA2 = $('#optA2').val();
           var optB1 = $('#optB1').val();
           var optB2 = $('#optB2').val();
           var other_qual1 = $('#other_qual1').val();
           var other_qual2 = $('#other_qual2').val();
           var interview1 = $('#interview1').val();
           var interview2 = $('#interview2').val();
           var total1 = $('#total1').val();
           var total2 = $('#total2').val();

           $.ajax({
               type: "POST",
               cache: false,
               url: "update.php",
               data: {
                   id1_text: name1,
                   id2_text: name1,
                   name1_text: name1,
                   name2_text: name2,
                   optA1_text: optA1,
                   optA2_text: optA2,
                   optB1_text: optB1,
                   optB2_text: optB2,
                   other_qual1_text: other_qual1,
                   other_qual2_text: other_qual2,
                   interview1_text: interview1,
                   interview2_text: interview2,
                   total1_text: total1,
                   total2_text: total2
               },
               success: function (data) {
                   alert('data has been updated!');
               }
           });
       });
   });
   </script>

$(文档).ready(函数(){
$(“#更新”)。单击(函数(e){
e、 预防默认值();
var id1=$('#id1').val();
var id2=$('#id2').val();
var name1=$('#name1').val();
var name2=$('#name2').val();
var optA1=$('#optA1').val();
var optA2=$('#optA2').val();
var optB1=$('#optB1').val();
var optB2=$('#optB2').val();
var other_qual1=$('#other_qual1').val();
var other_qual2=$('#other_qual2').val();
var interview1=$('#interview1').val();
var interview2=$('#interview2').val();
var total1=$('#total1').val();
var total2=$('#total2').val();
$.ajax({
类型:“POST”,
cache:false,
url:“update.php”,
数据:{
id1_text:name1,
id2_文本:名称1,
名称1_文本:名称1,
名称2_文本:名称2,
optA1_文本:optA1,
optA2_文本:optA2,
optB1_文本:optB1,
optB2_文本:optB2,
其他资格文本:其他资格,
其他资格文本:其他资格,
访谈1_文本:访谈1,
访谈2_文本:访谈2,
总计1_文本:总计1,
总计2_文本:总计2
},
成功:功能(数据){
警报('数据已更新!');
}
});
});
});
PHP

<?php
if (isset($_POST['id1_text'])) {

    mysql_connect("localhost", "root", "") or die("cant connect to database!");
    mysql_select_db("test") or die("cant find database!");
    $id1         = $_POST['id1_text'];
    $id2         = $_POST['id2_text'];
    $name1       = $_POST['name1_text'];
    $name2       = $_POST['name2_text'];
    $optA1       = $_POST['optA1_text'];
    $optA2       = $_POST['optA2_text'];
    $optB1       = $_POST['optB1_text'];
    $optB2       = $_POST['optB2_text'];
    $other_qual1 = $_POST['other_qual1_text'];
    $other_qual2 = $_POST['other_qual2_text'];
    $interview1  = $_POST['interview1_text'];
    $interview2  = $_POST['interview2_text'];
    $total1      = $_POST['total1_text'];
    $total2      = $_POST['total2_text'];

    $query1 = mysql_query("UPDATE score SET name=$name1, score1=$optA1, score2=$optB1, other_qual=$other_qual1, interview=$interview1, total=$total1 WHERE id=$id1");
    $resource1 = mysql_query($query1) or die(mysql_error());

    $query2 = mysql_query("UPDATE score SET name=$name2, score1=$optA2, score2=optB2, other_qual=$other_qual2, interview=$interview2, total=$total2 WHERE id=$id2");
    $resource2 = mysql_query($query2) or die(mysql_error());
}

?>


您能告诉我代码的哪一部分是为了什么吗?或者更详细地描述一下您的问题。@i\u m\u可选我的更新代码不起作用?你能帮我吗?请让我试试……对不起,我睡着了,我没有立即回复。是的,javascript中的警报说它已更新,但我查看的数据库没有更新。问题出在查询上,因为在你的代码中,你将mysq_查询放在$query1和$query2变量上,然后再放在$resource变量中,我只需要删除它它…现在起作用了谢谢:D