Rahul Pamnani Rahul Pamnani - 2 months ago 7
jQuery Question

Not able to update columns in database

This is my userprofile.php.I have update button in this page.When I will click on this button the table will hide and form will show.But the ajax.php when i click on update profile button it will redirect me to this page without running update query in ajax.php page.

<?php
session_start();
if(isset($_SESSION['email']))
{
include('config.php');
include('userheader.php');
include('usersiderbar.php');;
?>
<div id="page-wrapper">
<div class="col-md-12 graphs">
<div id="form4" class="xs">
<h3><strong>Your Profile</strong></h3>
<div class="bs-example4" data-example-id="simple-responsive-table">

<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th><strong>Name</strong></th>
<th><strong>Email</strong></th>
<th><strong>Phone Number</strong></th>

</tr>
</thead>
<tbody>
<?php

$query1=mysql_query("select * from userregister where email='".$_SESSION['email']."'");

while($query2=mysql_fetch_array($query1))
{
?>
<tr>
<th scope="row"><?php echo $query2['name'];?></th>
<td><?php echo $query2['email'];?></td>
<td><?php echo $query2['phone'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div><!-- /.table-responsive -->

<div style="text-align:center;"><input type="button" name="upd4" id="upd4" class="btn-success btn" value="Edit Profile" /></div>

</div>

</div>

<div id='updateprofile'>

</div>

</div>
</div>
<!-- /#page-wrapper -->
<?php
include('adminfooter.php');
}
else {
$_SESSION['invalid']=='set';
header('location:login.php');
}
?>
<script>
$(document).ready(function(){
$("#upd4").click(function(){

$.ajax({
type: "POST",
url: "ajax.php",
data: $("form").serialize(),
success: function(data) {
$("#form4").hide();
$("#updateprofile").show();
$("#updateprofile").append(data);
}
});

});

});

</script>


I am updating database but it is not showing any updation.Firstly I have user details and edit button.By clicking on edit button one form will open using ajax.

<?php
session_start();
include('config.php');
$query2 = "SELECT * FROM userregister where email='".$_SESSION['email']."'";
$result2 = mysql_query($query2);
$row2 = mysql_fetch_array($result2);

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

$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];

$sql="UPDATE userregister SET name='$name',email='$email',phone='$phone' where email='".$_SESSION['email']."'";
$res=mysql_query($sql);

echo "<script>alert('Your Record Sucessfully Updated.');</script>";
}
?>
<h3>Update Your Profile</h3>
<div class="tab-content">
<div class="tab-pane active" id="horizontal-form">

<form class="form-horizontal" action="" method="post">
<div class="form-group">
<label class="col-sm-2 control-label">Name :- </label>
<div class="col-sm-8">
<input type="text" name="name" id="name" class="form-control1" value="<?php echo $row2['name']; ?>" tabindex="1" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Email :- </label>
<div class="col-sm-8">
<input type="text" name="email" id="email" class="form-control1" value="<?php echo $row2['email']; ?>" tabindex="1" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Phone Number :- </label>
<div class="col-sm-8">
<input type="text" name="phone" id="phone" class="form-control1" value="<?php echo $row2['phone']; ?>" tabindex="1" />
</div>
</div>

<div style="text-align:center;"><button class="btn-success btn" name="updatenew" id="updatenew">Update Profile</button></div>

</form>
</div>
</div>


I have 5 columns in database but I am updating only 3 by selecting particular email id.It is showing value in input box but when i am changing the values in input box it is not showing any message .
This is my userregister.php where i have button.By clicking on that button it will show me ajax.php.

Answer

please add a new line in your form

    <div class="tab-content">
                    <div class="tab-pane active" id="horizontal-form">

                        <form class="form-horizontal" action="" method="post">
                           <input type="hidden" name="updatenew" value="Update Profile" />
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Name :- </label>
                                <div class="col-sm-8">
                                    <input type="text" name="name" id="name" class="form-control1" value="<?php echo $row2['name']; ?>" tabindex="1" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Email :- </label>
                                <div class="col-sm-8">
                                    <input type="text" name="email" id="email" class="form-control1" value="<?php echo $row2['email']; ?>" tabindex="1" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Phone Number :- </label>
                                <div class="col-sm-8">
                                    <input type="text" name="phone" id="phone" class="form-control1" value="<?php echo $row2['phone']; ?>" tabindex="1" />
                                </div>
                            </div>

                          <div style="text-align:center;"><button class="btn-success btn" name="updatenew" id="updatenew">Update Profile</button></div> 

                        </form>
                     </div>
         </div>

  <script>
     $(document).on("click", "#updatenew", function(){

       $.ajax({
            type: "POST",
            url: "ajax.php",
            data: $("form").serialize(),
            success: function(data) {
              alert(data);
              $("#updateprofile").hide();
            }
  });

  });
 </script>

And change your button name hope it will work for you :).