I want to do system comment for every post as facebook, system comment simple but my problem if I do this code I get the data-id value the first post
code jquery
$('.reponse').each(function(j){
$(this).attr('id','reponse_'+j);
});
code html+php
$stmt = $connect->query('SELECT *FROM publications
ORDER BY date_post DESC ');
while($row=$stmt->fetch() )
{?>
<div class ="reponse" data-id='<?php echo trim(htmlspecialchars($row['id_post'])); ?>'> </div>
<?php }?>
code ajax
$('#form_post').on('submit',function(e) {
e.preventDefault();
var query = $('#comments').val();
var queryid = $('.reponse').data('id');
$.ajax({
method : 'POST',
data : {commentPost:query,idpost:queryid},
url : 'traitementCommentPost.php',
success : function(data)
{
$('.fetch_all_comment').prepend(data) ;
}
});
$('#comments').val('');
});
always i get data-id value the first post
Related
I'm looking to change the content of a div based on a link being clicked on a php page. The link is created through a loop and I need to pass several parameters through the URL. The div is also created through the loop so the id of the div will be variable. I'm having trouble with the AJAX to make this happen.
Below is my php:
<?php
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
$tripDestination = $row3["tripDestination"];
$sessionID = $row3["$sessionID"];
$price = $row3["price"];
echo "" . $tripDestination . ' - ' . $price . "";
echo "<br />";
echo "<div id=\"trips\"></div>";
}
}
?>
I need to pass two variables in the URL: sessionID and tripDestination. I was able to load static content, but it needs to be dynamic. Here's my AJAX so far
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({url: "sqlUpload.php?sessionID=35&tripDestination=SFO", success: function(result){
$("#div1").html(result);
}});
});
});
</script>
Thanks in advance!
I might think about sending the information from a data attribute on the link:
PHP:
<?php
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
$tripDestination = $row3["tripDestination"];
$sessionID = $row3[$sessionID];
$price = $row3["price"];
// Store the organized data
$data = array(
'tripDestination'=>$tripDestination,
'sessionID'=>$sessionID,
'price'=>$price
);
?>
<!-- You can store the array into json on the data attribute -->
<a href="#" class="data-set" data-information='<?php echo json_encode($data) ?>'><?php echo $tripDestination.' - '.$price ?></a>
<br />
<div class="data-response"></div>
<?php
}
}
?>
JavaScript:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
// When user clicks the <a> that has the "data-set" class
$('.data-set').on('click',function(e){
// I like to prevent default here, just incase
e.preventDefault();
// Assign current obj
var getObj = $(this);
// Fetch the json from the attribute
var getData = getObj.data('information');
// Send
$.ajax({
// Just send to the page, no query string
url: "sqlUpload.php",
// I would send POST, personally
type: 'GET',
// This is the data being sent
data: getData,
success: function(response){
// Presumably you want to put the response into the
// accompanying div, then you can just do next()
getObj.next('.data-response').html(response);
}});
});
});
</script>
I have a while loop which loads the posts of the user and when the like button is clicked to get the post id send it to another php page which will process it and update in the database the likes number but I can't get the good id..
My while loop :
while($posts = mysqli_fetch_array($query)){
<div <?php echo "id=".$posts['id'] ?>
<div class="post">
.
.
</div>
</div> }
And the Ajax Request:
var id = "<?php echo $posts['id'] ?>";
$.ajax({
type: 'POST',
url: 'likes.php',
data: {id:id},
success: function(msg){
alert('Success!');
}
});
But it output only the last value of the while loop and I want for every post to get the id when the like button is pressed.Thanks for help!
What you're doing would never work. Since you have multiple IDs, you have to retrieve the ID from the <div> that was clicked on, and pass that to your ajax call, e.g.
<div data-id="42" class="clickme">
<script>
$('div.clickme').click(function (e) {
id = $(this).attr('data-id');
$.ajax( .... use "id" here ... );
}
alternatively, even something like this would help:
<? while(... fetch from db ... ){ ?>
<div onclick="doAjax(<?php echo json_encode($row['id']); ?>)">
<script>
function doAjax(id) {
$.ajax(... use "id" here ...);
}
</script>
So Ive been struggling with this problem all day and can't seem to get around it.
I need to call a php query whenever an option from a dropdown menu is selected.
<select class="selectpicker" id="headSelector">
<?php
$cname = $_GET['cname'];
$linkID = mysql_connect("localhost","USER","PASS");
mysql_select_db("USR", $linkID);
$SQLCurr = "SELECT `AName` FROM `Char-Armor` WHERE `CName` = '$cname' AND `AType`= 'Head'";
$currHeadValues = mysql_query($SQLCurr, $linkID);
$currRow = mysql_fetch_row($currHeadValues);
$curr = $currRow[0];
if($curr == '' || $curr == NULL){
$curr = 'None';
}
$SQLHead = "SELECT AName FROM `Armor` WHERE AType = 'Head'";
$allHeadValues = mysql_query($SQLHead, $linkID);
echo "<option>".$curr."</option>";
while($row = mysql_fetch_assoc($allHeadValues)){
echo "
<option>".$row['AName']."</option>
";
}
?>
</select>
The php part needs to take the 'AName' from the option and use it to insert into a table.
I have done a lot of reading about AJAX but I do not quite understand how it is supposed to work. I think it is like html -> js -> Ajax -> php
I need it to stay on the same page when an option is selected.
Any explanation would be great, thanks!
Here's what you can do.
1). As soon as an option is selected, run a jquery onchange event and get the value of the selected option.
2). Now, run an ajax request with the value of the selected value and post this data to a backend php file.
3). Now on this backend php file, receive data and process (run the query).
Code Sample.
Change your option line in this way.
<option value="$row['AName']">".$row['AName']."</option>
jQuery-Ajax
$("#headSelector").change(function(e){
//get the value of the selected index.
value = $(this).val();
//make an ajax request now
$.ajax({
url: 'yourPhpBackendScript.php',
type: 'POST',
data: {value: value},
success:function(response)
{
alert(response);
}
})
})
yourPhpBackendScript.php
//You can now receive the selected value as $_POST['value'];
//get the value now
$value = $_POST['value'];
//you can apply validations if you want.
//Now, run the query and send a response. Response can be a simple message like data submitted etc. So
runQueryHere
echo "inserted"; //response returned to ajax rquest
First of all, return the string like this:
$options_arr = '';
while($row = mysql_fetch_assoc($allHeadValues)){
$options_arr .= "<option>".$row['AName']."</option>
";
}
echo $options_arr;
Use the change event like this:
$("#headSelector").change(function(){
$.ajax({
data: '',
url: 'your_url',
type: 'POST',//Or get
success: function(options_array){
$("#headSelector").empty().append(options_str);
}
});
});
I am trying to get the data from a database and populate then in a drop down in the code below.
I am unable to populate them as the data output says:
"undefined index: selectid" from php ma be the data is not passing from ajax or the php is unable to read.
HTML:
<select id="dynamicDropdown"></select>
JS:
var storedValue = localStorage.getItem("id");
$.ajax({
url : "DD.php",
data: {"storedValue":storedValue},
success : function(data){
alert(data);
console.log(data) // data should be an array that you have mentioned.
data.map(function(c){
$("#dynamicDropdown").append("<option value="+c.C_CO+">"+c.C_NAME1+"</option>");
});
}
});
};
PHP:
$mysqli=mysqli_connect('xxx','xxx','xxx','xxx');
$selectid = mysqli_real_escape_string($mysqli,trim($_POST['storedValue']));
$query112 ="SELECT * FROM dd_table WHERE id='$selectid'";
$result112 = mysqli_query($mysqli,$query112)or die(mysqli_error());
$num_row112 = mysqli_num_rows($result112);
while($row=mysqli_fetch_array($result112))
{
$response = array($row['dd_data'] );
echo json_encode($response);
}
I might be wrong but shouldn't you write it like this :
$query112 ="SELECT * FROM dd_table WHERE id='".$selectid."'"; ?
You don't need any jQuery for that:
Try this:
<select>
<?php
foreach(mysqli_fetch_array($result112) as $x){
echo "<option>". $x['dd_data']. "</option>";
}
?>
</select>
I have some URL mysite.com/json.php, which returns something like this : [{"invoice_number":"INV#20101"}]
on another page I have a <input type="hidden" id="myinvoice" />
I just wanted to set that invoice_number value to this hidden field withJQuery. How can I do this?
on JSON page I have converted JSON with this code :
<?php
$return_arr = array();
$fetch = mysql_query("SELECT invoice_number FROM db_stocks ORDER BY stock_id DESC LIMIT 1 ");
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['invoice_number'] = $row['invoice_number'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
?>
You can use jQuery.ajax() to get the returned array then set the value.
$.ajax({
url: "json.php",
success: function(data) {
$("#myinvoice").val(data[0].invoice_number);
}
});
You can use the following jquery:
$.get('mysite.com/json.php', function(data){
$('#myinvoice').val(data[0].invoice_number);
} 'json');
Also please don't use mysql but use pdo or mysqli instead, see why-shouldnt-i-use-mysql-functions-in-php for more information about this.