简体   繁体   English

在 PHP 循环中的表单提交上用 jQuery 替换 div 内容

[英]Replace div contents with jQuery on form submit within PHP loop

I have this jQuery form being outputted in a PHP while loop if the button has not previously been clicked and just an image with the value if it has, the function is like facebooks like button where when the user clicks the button the icon changes so its not clickable any longer and the value increments by 1. The form submission works but I cannot seem to update the icon image and value count in the feed without effecting all the other buttons and values in the feed… I tried jQuery replaceWith() but it replaces all the #bumpCont divs in the feed…我有这个 jQuery 表单在 PHP while 循环中输出,如果该按钮之前没有被点击过,并且只有一个带有该值的图像,如果有的话,该功能就像 facebooks 一样的按钮,当用户点击按钮时,图标会改变,所以它的不再可点击并且值增加 1。表单提交有效,但我似乎无法更新提要中的图标图像和值计数而不影响提要中的所有其他按钮和值......我尝试了 jQuery replaceWith()但它替换#bumpCont中的所有#bumpCont div...

index.php索引.php

<div class="images">
<?php
while($row = $result2->fetch_assoc()){
    $path = $row['path'];
    $user = $row['user'];
    $id = $row['id'];
    $desc = $row['desc'];
    $update = $row['update_date'];
    $bump = $row['bump'];
    $timeFirst  = strtotime($date);
    $timeSecond = strtotime($update);
    $timeSecond = $timeSecond + 86400;
    $timer = $timeSecond - $timeFirst;

?>
<?php if(empty($desc)){}else{?><div id="desc"><?php echo $desc;?></div><?php }?>
<img id="pic" src="uploads/<?php echo $path;?>"/>
<div id="userCont">
<div id="user"><a rel="external" href="user_profile.php?user='.$user.'"><?php echo $user;?></a></div>
<div id="timer"><?php echo $timer;?></div>


<?php
if(in_array($path, $mypath)) {
    echo '<div id="bumpCont"><img id="bump" style="height:55px;right:8px;top: 2px;position: relative;" src="../img/bumpg.png"/><span id="bumpCount">'.$bump.'</span></div>';
}else{
    echo '<form method="post" id="bumpF" data-ajax="false">';
    echo '<input name="id" data-ajax="false" id="field_'.$id.'" type="hidden" value="'.$id.'" />';
    echo '<div id="bumpCont"><input type="image" style="height:55px;right:8px;top: 2px;position: relative; " id="bump" src="../img/bump.png" id="searchForm" onclick="SubmitForm('.$id.');" value="Send" /><span id="bumpCount">'.$bump.'</span></div>';
    echo ' </form>';
 }
?>

</div>

<?php
}
?>

//Submit Form
function SubmitForm(id) {
event.preventDefault();
var name = $('#field_'+id).val();
console.log(name);
$.post("bump.php", {name: name},
function(data) {
$( "#bumpCont" ).replaceWith( '<div id="bumpCont"><img id="bump" style="height:55px;right:8px;top: 2px;position: relative;" src="../img/bumpg.png"/><span id="bumpCount">' + data + '</span></div>' );
}

Bump.php -凹凸.php -

$id =  $_POST['name'];
$sessionUser = $_SESSION['userSession'];

// GET USERNAME
$sql = "SELECT * FROM userbase WHERE user_id='$sessionUser'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    $myname = $row['username'];
   }

}


$bump = 1;              
$sql = "SELECT * FROM images WHERE id=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
      $bump = $row['bump'];
      $path = $row['path'];
      $desc = $row['desc'];
      $post_user = $row['user'];
        $bump++;
    }

}

$bumpC = 0;                 
$sql = "SELECT * FROM bumped WHERE path='$path' AND myname ='$myname'";
$result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $bumpC++;
    }

}

echo $bump;

if($bumpC >= 1){

}else{
$sql = "INSERT INTO `bumped` ( `myname`,`path`, `description`, `post_user`) VALUES ( '$myname','$path', '$desc', '$post_user')";

if ($conn->query($sql) === TRUE) {

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$sql = "UPDATE images SET update_date='$date' WHERE id=$id";

if ($conn->query($sql) === TRUE) {

} else {
echo "Error updating record: " . $conn->error;
}

$sql = "UPDATE images SET bump=$bump WHERE id=$id";

if ($conn->query($sql) === TRUE) {

} else {
echo "Error updating record: " . $conn->error;
}
}

First look shows me a problem of Elements with same ID in loop .第一次看向我展示了Elements with same ID in loopElements with same ID in loop的问题。

You could have same class to multiple elements.您可以对多个元素使用相同的类。

<div class="bumpCont"><span class="bumpCount">1</span></div>
<div class="bumpCont"><span class="bumpCount">2</span></div>

Use $(this)使用$(this)

Based on the click on particular element, you can change contents.基于对特定元素的点击,您可以更改内容。

$('.bumpCount').click(function(){
  $(this).html(parseInt($(this).html) + 1);
});

Hope this helps you.希望这对你有帮助。

 $('.bumpCount').click(function(){ $(this).html(parseInt($(this).html()) + 1); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="bumpCont"><span class="bumpCount">1</span></div> <div class="bumpCont"><span class="bumpCount">2</span></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM