mysql数据库更新与ajax php无法正常工作

时间:2016-05-21 14:23:19

标签: php jquery mysql ajax

我试图用按钮点击更新mysql数据库表。但数据库没有得到更新......

HTML代码:

<tr >
<td>Advt Heading :</td>
<td>
<input type="hidden" name="idnew" id="idnew" value="<?=$member_data['id']?>"> //retrieved from mysql database 

<input type="text" name="advt_headingnew" id="advt_headingnew" value="<?=stripslashes($member_data['advt_heading']);?>" /> //retrieved from mysql database ...**I want to edit its previus retreived value...and update database**

<input name="submit" type="button" id="submit" value="Update" />

</td>
</tr>

脚本:

<script src="http://code.jquery.com/jquery.min.js"></script>  
<script>
$(document).ready(function () { 
$('#submit').click(function(){
var advt_headingnew = $("#advt_headingnew").val();
var idnew = $("#idnew").val();


$.ajax({
     type:'POST',
     url:'update-advt-heading.php',
     data: "advt_headingnew="+advt_headingnew+"&idnew="+idnew,

     success:function( msg ) {
     alert( "Data Saved: " + msg );
        }

 });
});
});


</script>

PHP - update-advt-heading.php代码:

<?
$user_name = "databaseusername";
$password = "databasepassword";
$database = "databasename";
$server = "localhost";

mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database);

$heading = $_POST['advt_headingnew'];
$id=$_POST["idnew"];

if (isset($_POST['submit'])){


   $queryStr = "UPDATE tablename SET advt_heading='$heading' WHERE id='$id'";
    if ( mysql_query($qyeryStr)){
        return "success!";
    }else{
        return "failed!";
    }
}

?>

2 个答案:

答案 0 :(得分:0)

将您的js代码更改为此

    $(document).ready(function () { 
        $('#submit').click(function(){
           var advt_headingnew = $("#advt_headingnew").val();
           var idnew = $("#idnew").val();
           var submitval = $(this).val();

           $.ajax({
             type:'POST',
             url:'update-advt-heading.php',
             data: "advt_headingnew="+advt_headingnew+"&idnew="+idnew+"&submit="+submitval,

             success:function( msg ) {
                alert( "Data Saved: " + msg );
             }

          });
       });
   });

答案 1 :(得分:0)

我已经改变了

var advt_headingnew = $("#advt_headingnew").val();
var idnew = $("#idnew").val();

var advt_headingnew = document.getElementsByName("advt_headingnew")[0].value;
var idnew = document.getElementsByName("idnew")[0].value;

现在正在运作..