0

I am trying to update mysql database table with button click. But database is not getting updated...

HTML Code :

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

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

<?
$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!";
    }
}

?>
1
  • You are not sending submit value through ajax that's why your if condition fails. As a NOTE: Don't use mysql extension as it is depricated and not even supported in php7. You should use mysqli instead. Commented May 21, 2016 at 14:34

2 Answers 2

0

Change your js code to this

    $(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 );
             }

          });
       });
   });
Sign up to request clarification or add additional context in comments.

6 Comments

Are you getting all values correctly through ajax in php script ??
try changing <? to <?= or <?php
got it you misspelled $queryStr in mysql_query(..)
what i think ....update_advt_heading.php is not getting value of advt_headingnew and idnew...
|
0

I have changed

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

To

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

NOW IT IS WORKING..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.