how to edit data using ajax jquery php mysqli - php

I am very confused by errors in this program, can you help me?
And this is a process WITH php:
$nm_pel = $_POST['nm_pelanggan'];
$alamat = $_POST['alamat'];
$telepon = $_POST['telepon'];
$email = $_POST['email'];
$id_pelanggan = $_POST['id_pelanggan'];
$sql = "UPDATE pelanggan SET nm_pelanggan='$nm_pel',alamat='$alamat',telepon='$telepon',email='$email' WHERE id_pelanggan='$id_pelanggan' ";
mysqli_query($link,$sql);
and this is a function ajax:
function updateData(str){
var id_pelanggan = str;
var nm_pelanggan = $('#nm-'+str).val();
var alamat = $('#alt-'+str).val();
var telepon = $('#tlp-'+str).val();
var email =$('#em-'+str).val();
$.ajax({
type : 'POST',
url : 'core/process.php?p=edit',
data : "nm_pelanggan="+nm_pelanggan+"&alamat="+alamat+"&telepon="+telepon+"&email="+email+"&id_pelanggan="+id_pelanggan,
success : function(data){
viewData();
}
});
}

Your javascript ajax function appears misformatted. May I recommend correcting the constructor for the data you send as follows? Hope this helps.
$.ajax({
type : 'POST',
url : 'core/process.php?p=edit',
data : {nm_pelanggan:nm_pelanggan, alamat:alamat, telepon:telepon, email:email, id_pelanggan:id_pelanggan},
success : function(data){
viewData();
}
});

and this code form edit data :
<div class="modal fade" id="edit-<?php echo $row['id_pelanggan']; ?>" tabindex="-1" role="dialog" aria-labelledby="editLabel-<?php echo $row['id_pelanggan']; ?>">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id=editLabel-"<?php echo $row['id_pelanggan'];?>">Edit Data Pelanggan <?php echo $row['id_pelanggan']; ?> </h4>
</div>
<form>
<div class="modal-body">
<div class="form-group ">
<label class="control-label requiredField" for="id_pelanggan">
Kode Pelanggan
<span class="asteriskField">
*
</span>
</label>
<input class="form-control" id="<?php echo $row['id_pelanggan']; ?>" value="<?php echo $row['id_pelanggan']; ?>" type="text" disabled>
</div>
<div class="form-group ">
<label class="control-label " for="nm_pelanggan">
Nama Pelanggan
</label>
<input class="form-control" id="nm-<?php echo $row['id_pelanggan']; ?>" value="<?php echo $row['nm_pelanggan']; ?>" type="text">
</div>
<div class="form-group ">
<label class="control-label " for="alamat">
Alamat
</label>
<input class="form-control" id="alt-<?php echo $row['id_pelanggan'];?>" value="<?php echo $row['alamat'];?>" type="text">
</div>
<div class="form-group ">
<label class="control-label " for="telepon">
Nomor HP
</label>
<input class="form-control" id="tlp-<?php echo $row['id_pelanggan'];?>" value="<?php echo $row['telepon'];?>" type="text">
</div>
<div class="form-group ">
<label class="control-label " for="email">
Email
</label>
<input class="form-control" id="em-<?php echo $row['id_pelanggan'];?>" value="<?php echo $row['email']; ?>" type="email">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Batal</button>
<button type="submit" onclick="updateData(<?php echo $row['id_pelanggan']; ?>)" class="btn btn-primary" >Edit Data</button>
</div>
</form>
</div>
</div>
</div>
</div>

Related

How to Update Mysql Data with a button on Home Page that is passing id on URL?

I have created an Edit Item popup Bootstrap modal on home page. Now what I want is to update the selected item details using the PHP.
Here i am passing item id through the URL in order to select the item
Now when I press edit Button it opens a Popup with all the details in it( already filled) but when I try to click on Save Changes Button it just do nothing and My data not updated. I have also used POST method but its not working.
Here is the Button that opens the Popup Modal
<?php
$res= mysqli_query($db,"SELECT* FROM `books`;");
if(mysqli_num_rows($res)>0){
while($row= mysqli_fetch_assoc($res)){
if($row!=0)
{
$bid= $row['book_id'];
?>
<ul>
<li>
<a onclick="$('#editModal<?php echo $row['book_id']?>').modal('show');" class="btn-show-modal edit-btn" data-toggle="modal"><i style="padding-right: 140px;" class="fas fa-edit"></i></a>
</li>
<!--Here is the Popup Modal for Edit -->
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="updatebook.php" method="POST">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" name="save">Save Changes</button>
</div>
</div>
</div>
</div>
<?php
}
}
}
else
{
echo"<h1 style='color: white;font-size:58px; font-family: Poppins; font-weight: 600;' class=m-2>U!Oh Nothing Found Here!</h1>
<button onclick=\"window.location.href='add_books_form.php'\" class=\"btn btn-info bg-primary m-5\">Contribute</button>";
}
?>
</ul>
Here is the updatebook.php Page
<?php
include "connection.inc.php";
if(isset($_POST['save']))
{
$id= $_POST['id'];
$isbn= $_POST['isbn'];
$title= $_POST['title'];
$author= $_POST['author'];
$image= $_POST['image'];
$desc= $_POST['description'];
$url= $_POST['url'];
$res = mysqli_query($db, "UPDATE books SET isbn= '$isbn', title= '$title', author= '$author',image= '$image', description= '$desc', url= '$url' WHERE book_id= '$id'");
?>
if($res)
{
<script type="text/javascript">
alert("Data Updated Successfully");
window.location.href= "home.php";
</script>
}
else
{
<script>
alert("Not Updated!");
window.location.href="home.php";
</script>
}
<?php
}
?>
Please Help me to solve this Update Issue i am facing for a long time. I have researched all through but didn't got any solution.
Change your Save button type to "submit" and remove the <a> hyperlink.
Use <form> as a part of both modal-body and modal-footer so the submit button will be part of it.
Take book_id in a hidden field so it will be the part of POST.
Look at the modal code below:
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="updatebook.php" method="POST">
<input type="hidden" name="id" value="<?php echo $row['book_id'];?>">
<div class="modal-body">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save">Save Changes</button>
</div>
</form>
</div>
</div>
</div>

Could not remove using parent div id in Jquery

I facing one issue. I need to remove total div using the id in Jquery while clicking on minus button. I am explaining my code below.
<?php foreach($module as $item){ ?>
<div id="planWithfriends<?php echo $item->id; ?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">PLAN WITH FRIENDS</h4>
</div>
<div class="modal-body">
<form class="row" action="" method="POST">
<div class="input_fields_wrap<?php echo $item->id; ?>">
<div id="parentid<?php echo $item->id; ?>">
<div class="form-group col-md-4">
<label class="go-right" style="margin-bottom:0px;">Email </label>
<input name="email_<?php echo $item->id; ?>[]" id="email_<?php echo $item->id; ?>" type="text" class="form-control input_style1" placeholder="" value="" style="height:34px;">
</div>
<div class="form-group col-md-4">
<label class="go-right" style="margin-bottom:0px;">Phone </label>
<input name="phone_<?php echo $item->id; ?>[]" id="phone_<?php echo $item->id; ?>" type="text" class="form-control input_style1" placeholder="" value="" style="height:34px;">
</div>
<div class="form-group col-md-4">
<button type="button" class="add_button_<?php echo $item->id; ?> btn btn-success" style="height:34px;margin-top: 20px;"><i class="fa fa-plus"></i></button>
</div>
</div>
</div>
<div class="clear"></div>
<div class="form-group col-md-12 text-right">
<button style="margin-top:25px;" type="button" class="btn btn-action" data-dismiss="modal" id="planbutton<?php echo $item->id; ?>">Submit</button>
</div>
<div class="clear"></div>
</form>
</div>
<!--<div class="modal-footer" style="text-align:center;">
<button type="button" class="btn btn-action" data-dismiss="modal">Close</button>
</div>-->
</div>
</div>
</div>
<script type="text/javascript">
$(function(){
var addButton = $('.add_button_<?php echo $item->id; ?>');
var wrapper = $('.input_fields_wrap<?php echo $item->id; ?>');
$(addButton).click(function(){
$(wrapper).append('<div id="parentid<?php echo $item->id; ?>"><div class="form-group col-md-4"><input name="email_<?php echo $item->id; ?>[]" id="email_<?php echo $item->id; ?>" type="text" class="form-control input_style1" placeholder="" value="" style="height:34px;"></div><div class="form-group col-md-4"><input name="phone_<?php echo $item->id; ?>[]" id="phone_<?php echo $item->id; ?>" type="text" class="form-control input_style1" placeholder="" value="" style="height:34px;"></div><div class="form-group col-md-4"><button type="button" class="remove_field<?php echo $item->id; ?> btn btn-danger" style="height:34px;margin-top: 20px;"><i class="fa fa-minus"></i></button></div></div>'); //
})
$(wrapper).on('click', '.remove_field<?php echo $item->id; ?>', function(e){
e.preventDefault();
$(this).parent('#parentid<?php echo $item->id; ?>').remove();
})
})
</script>
<?php } ?>
Here I creating another row clicking on + button but when user will click on - button the respective row should remove which is not happening in my case. Please help me to resolve this issue.
As I have seen you put JavaScript code inside the loop. Please put it outside the loop as:
<script type="text/javascript">
$(function(){
var addButton = $('.add_button');
var wrapper = $('.input_fields_wrap');
$(addButton).click(function(){
$(wrapper).append('<div id="parentid"><div class="form-group col-md-4"><input name="email_<?php echo $item->id; ?>[]" id="email_<?php echo $item->id; ?>" type="text" class="form-control input_style1" placeholder="" value="" style="height:34px;"></div><div class="form-group col-md-4"><input name="phone_<?php echo $item->id; ?>[]" id="phone_<?php echo $item->id; ?>" type="text" class="form-control input_style1" placeholder="" value="" style="height:34px;"></div><div class="form-group col-md-4"><button type="button" class="remove_field<?php echo $item->id; ?> btn btn-danger" style="height:34px;margin-top: 20px;"><i class="fa fa-minus"></i></button></div></div>'); //
})
$(wrapper).on('click', '.remove_field', function(e){
e.preventDefault();
$(this).parent('.parentid').remove();
})
})
</script>
and give a class remove_field when clicking on it get parent div class and do whatever you want. This is not good practicing to put JavaScript inside the PHP loop.

Modal not updating data on row 2 and so on

Hi so the modal works fine on the first row but when i clicked on the 2nd row modal doesnt fetch any data
The form where it fetches the data , this thing works on first page only or either no
$ID = $_POST['ID'];
$sql = "SELECT * FROM `unity_users` WHERE `uid` = '$ID'";
$res = mysqli_query($con,$sql);
if(mysqli_num_rows($res)>0){
$row = mysqli_fetch_assoc($res);
$htmlToReturn = '<form action="" enctype="multipart/form-data" class="formTopMargin" autocomplete="off" method="post">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<div class="form-group">
<label for="cat">User Name:</label>
<input type="text" class="form-control" value="'.$row['name'].'" name = "name">
</div>
<div class="form-group">
<label for="cat">Email:</label>
<input type="email" class="form-control" value="'.$row['email'].'" name = "email">
</div>
<div class="form-group">
<label for="cat">Score:</label>
<input type="number" class="form-control" value="'.$row['score'].'" name = "score">
</div>
<div class="form-group">
<label for="cat">PPA:</label>
<input type="text" class="form-control" value="'.$row['ppa'].'" name = "ppa">
</div>
<div class="form-group">
<label for="cat">Hash:</label>
<input type="text" class="form-control" value="'.$row['hash'].'" name = "hash">
</div>
<div class="form-group">
<input type="hidden" hidden value="'.$row['uid'].'" name = "uid">
</div>
</div>
<div class="col-sm-3"></div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<button type="submit" name="update" class="btn loginButton">Update</button>
</div>
<div class="col-sm-3"></div>
</div>
</form>';
The Output of the code
<?php
$sql = "SELECT * FROM `unity_users`";
$res = mysqli_query($con,$sql);
if(mysqli_num_rows($res)>0){
while ($row = mysqli_fetch_assoc($res)) {
echo '<tr>';
echo '<td>'.$row['uid'].'</td>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['email'].'</td>';
echo '<td>'.$row['score'].'</td>';
echo '<td>'.$row['ppa'].'</td>';
echo '<td>'.$row['hash'].'</td>';
if($row['payment']=="Released"){
$payment = "requested";
}else{
$payment = "pending";
}
echo '<td>'.$payment.'</td>';
echo '<td class="pointer"><i id="delete_'.$row['uid'].'" class="fa fa-trash fa-2x details" aria-hidden="true"></i></td>';
echo '<td class="pointer"><i data-toggle="modal" data-target="#myModal" id="edit_'.$row['uid'].'" class="fa fa-pencil-square-o fa-2x details" aria-hidden="true"></i></td>';
echo '</tr>';
}
}
?>
The modal
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 style="text-align: center;margin-top: 1%;font-size: 25px;" class="modal-title">User Data</h4>
</div>
<div class="modal-body" id="categoryModel">
</div>
</div>
</div>
First row which is working
Updating data is working
Page 2 second row not working

Insert records to mysql database with php using Ajax

I am trying to added record using a modal. I have use the following code but my record is not added into the database.. help me through pls
<link rel="stylesheet" type="text/css" href="/Project/Style/bootstrap.css"/>
<script src="Style/jQuery/jquery-1.11.1.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<?php
include('header.php');
require_once('Connections/conn.php');
$sql2= "SELECT * from type where cid='1' ";
$results =mysqli_query($conn,$sql2);
?>
<div id="main">
<div class="container" >
<div class="row">
<div class="form-group col-md-3">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
++Add Farm Product
</button>
</div>
<div class="input-group col-md-4 col-sm-offset-2">
<span class="input-group-addon">Search</span>
<input type="text" class="form-control" name="search-text" id="search-text" placeholder="Search By Product Name">
</div>
</div>
<div id="result"></div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Farm Product</h4>
</div>
<div class="modal-body">
<form>
<div class="row">
<div class="col-md-6 padding-top-10">
<label for="proname" class="control-label">Product Name:</label>
<input type="text" class="form-control" id="proname" placeholder="Product Name" name="proname" />
</div>
<div class="col-md-5 padding-top-10">
<label for="pro-type" class="control-label">Product Type:</label>
<select class="form-control" id="pro_type" name="pro_type" >
<option>Select Product type...</option>
<?php foreach($results as $result): ?>
<option value= " <?php echo $result['tid']; ?>" > <?php echo $result['type_name']; ?>
</option>
<?php endforeach ;?>
</select>
</div>
</div><br/>
<div class="row">
<div class="col-md-4 padding-top-10">
<label for="qty" class="control-label">Quantity:</label>
<input type="text" class="form-control" id="qty" placeholder="Quantity" name="qty" />
</div>
<div class="col-md-4 padding-top-10">
<label for="price" class="control-label">Unit Price :</label>
<input type="text" class="form-control" id="price" placeholder="Unit Price" name="price" />
</div>
<div class="col-md-4 padding-top-10">
<label for="pdate" class="control-label"> Production Date :</label>
<input type="date" class="form-control" id="pdate" placeholder="Production Date" name="pdate" />
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" >Close</button>
<button type="button" id="add" name="add" class="btn btn-primary">Add Product</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
// haddling the post for adding record
$('#add').click(function(){
var proname = $('#proname').val();
var pro_type = $('#pro_type').val();
var qty = $('#qty').val();
var price = $('#price').val();
var pdate = $('#pdate').val();
var datas="proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate"+pdate;
$.ajax({
type: "POST",
url: "addpro_exec.php",
data: datas,
success:function(data){
},
error: function(){
}
});
});
</script>
The code handling the posted data is below
//addpro_exec.php file
<?php
require_once('Connections/conn.php');
if(isset($_POST['add'])){
$proname=$_POST['proname'];
$pro_type= $_POST['pro_type'];
//$typname=$_POST['type_name'];
$qty= $_POST['qty'];
$price= $_POST['price'];
$pdate= $_POST['pdate'];
$asdate = \date('F');
$sql="INSERT into product VALUES('','$proname','','$qty','$price','$pdate','','$pro_type','1') ON DUPLICATE KEY UPDATE qty = qty+$qty";
$sql2= "INSERT into year_estm VALUES('','$proname','$asdate','$qty','') ON DUPLICATE KEY update Production_input=Production_input+$qty ";
$AddProduction= mysqli_query($conn, $sql2) or die('Cannot add to production'. mysqli_error($conn));
$RecAdd= mysqli_query($conn,$sql) or die('Cannot add Product' . mysqli_error());
if($RecAdd){
header('location:viewfarm.php');
}
}
else{
}
?>
Try the following line in your javascript:
var datas="add=1&proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate="+pdate;
That should fix the problem.
WARNING: Always filter and a properly quote user input before inserting into a database in order to avoid SQL injection hacks. Use prepared statements to help achieve this.
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

Php Private Message reply only functioning on the last row?

I am wondering why the code only works on the last row of the message reply, I can't reply on the latest message but I can reply on the last message, the messages are descending.
Please take a look at messages.php:
<form action="read_message.php" method="post">
<div class="pull-right">
<button class="btn btn-info" name="read"><i class="icon-check"></i> Read</button>
Check All <input type="checkbox" name="selectAll" id="checkAll" />
<script>
$("#checkAll").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</div>
<ul class="nav nav-pills">
<li class="active"><i class="icon-envelope-alt"></i>inbox</li>
<li class=""><i class="icon-envelope-alt"></i>Send messages</li>
</ul>
<?php
$query_announcement = mysql_query("select * from message_received
LEFT JOIN user ON user.user_id = message_received.user_id
where message_received.receiver_id = '$session_id' order by date_sent DESC
")or die(mysql_error());
$count_my_message = mysql_num_rows($query_announcement);
if ($count_my_message != '0') {
while ($row = mysql_fetch_array($query_announcement)) {
$id = $row['message_id'];
$id_2 = $row['message_id'];
$fn = $row['firstname'];
$ln = $row['lastname'];
$status = $row['message_status'];
$sender = $row['user_id'];
$sender_name = $fn . ' ' . $ln;
$receiver = $row['receiver_id'];
?><div class="alert alert-info">
<div class="post" id="del<?php echo $id; ?>">
<div class="message_content">
<?php echo $row['content']; ?>
</div>
<div class="pull-right">
<?php if ($status == 'read') {
} else {
?>
<input id="" class="" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
<?php } ?>
</div>
<hr>
Send by: <strong><?php echo $fn . ' ' . $ln; ?></strong>
<i class="icon-calendar"></i> <? php echo $row['date_sent']; ?>
<div class="pull- right">
<a class="btn btn-link" href="#reply<?php echo $id; ?>" data-toggle="modal" ><i class="icon-reply"></i> Reply </a>
</div>
<div class="pull-right">
<a class="btn btn-link" href="#<?php echo $id; ?>" data-toggle="modal" ><i class="icon-remove"></i> Remove </a>
<?php include("remove_inbox_message_modal.php"); ?>
<?php include("reply_inbox_message_modal_user.php"); ?>
</div>
</div>
</div>
<?php }
} else { ?>
<div class="alert alert-info"><i class="icon- info-sign"></i> No Message Inbox</div>
<?php } ?>
</form>
NOW THIS is the function for read_message.php, I can't see any error, please help me.
<?php
if (isset($_POST['reply'])) {
$sender = $_POST['sender'];
$receiver = $_POST['receiver'];
$my_message = $_POST['my_message'];
mysql_query("insert into message_received (user_id,receiver_id,content,date_sent) values('$receiver','$sender','$my_message',NOW())")or die(mysql_error());
mysql_query("insert into message_sent (receiver_id,content,date_sent,user_id) values('$sender','$my_message',NOW(),'$receiver')")or die(mysql_error());
echo "<script> alert('Your message has been sent $my_message') </script>";
echo " <script>location.replace('messages.php')</script>";
?>
<script>
alert('Message Sent');
window.location = "messages.php";
</script>
<?php
}
?>
now this is the reply modal codes,
<!-- Modal -->
<div id="reply<?php echo $id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria- hidden="true">x</button>
<h3 id="myModalLabel">Reply</h3>
</div>
<div class="modal-body">
<center>
<div class="control-group">
<label class="control-label" for="inputEmail">To:</label>
<div class="controls">
<input type="hidden" name="sender" id="inputEmail" value="<?php echo $sender; ?>" readonly>
<input type="hidden" name="receiver" id="inputEmail" value="<?php echo $receiver; ?>" readonly>
<input type="hidden" name="uid" id="inputEmail" value="<?php echo $uid; ?>" readonly>
<input type="hidden" name="my_name" value="<?php echo $reciever_name; ?>" readonly>
<input type="text" name="name_of_sender" id="inputEmail" value="<?php echo $sender_name; ?>" readonly>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Content:</label>
<div class="controls">
<textarea name='my_message'></textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" name="reply" id="<?php echo $id; ?>" class="btn btn-success reply"><i class="icon-reply"></i> Reply</button>
</div>
</div>
</center>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close</button>
<button id="<?php echo $id; ?>" class="btn btn-danger remove" data- dismiss="modal" aria-hidden="true"><i class="icon-check icon-large"></i> Yes </button>
</div>
when I reply to a message the message is sent but it has no content but data from sender, date and time is saved in the database, but in the lowest row, it all functions well? please help me. thank you
You have it your message modal window in one form.
So your solution works only if you have only one message. If you have more messages when you send your response send all responses for each message (its one form) and each response content have same name.
For this reason your server obtains empty $_POST['my_message']
You need have for each response own form and it will works. like this:
<form action="read_message.php" method="post">
<!-- Modal -->
<div id="reply<?php echo $id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria- hidden="true">x</button>
<h3 id="myModalLabel">Reply</h3>
</div>
<div class="modal-body">
<center>
<div class="control-group">
<label class="control-label" for="inputEmail">To:</label>
<div class="controls">
<input type="hidden" name="sender" id="inputEmail" value="<?php echo $sender; ?>" readonly>
<input type="hidden" name="receiver" id="inputEmail" value="<?php echo $receiver; ?>" readonly>
<input type="hidden" name="uid" id="inputEmail" value="<?php echo $uid; ?>" readonly>
<input type="hidden" name="my_name" value="<?php echo $reciever_name; ?>" readonly>
<input type="text" name="name_of_sender" id="inputEmail" value="<?php echo $sender_name; ?>" readonly>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Content:</label>
<div class="controls">
<textarea name='my_message'></textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" name="reply" id="<?php echo $id; ?>" class="btn btn-success reply"><i class="icon-reply"></i> Reply</button>
</div>
</div>
</center>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close</button>
<button id="<?php echo $id; ?>" class="btn btn-danger remove" data- dismiss="modal" aria-hidden="true"><i class="icon-check icon-large"></i> Yes </button>
</div>
</form>
PHP variables are not parsed in single quotes. You need to update your SQL queries to use double quotes, or no quotes, with your PHP variables.
insert into message_sent (receiver_id,content,date_sent,user_id) values("$sender","$my_message",NOW(),"$receiver")
That is why only the date is saving.
Clear the messages in your database, update the queries (multiple) and try again. Then, report back with an update and I'll take another look at it.

Categories