【问题标题】:Change status php ajax and script and value input更改状态 php ajax 和脚本和值输入
【发布时间】:2021-06-03 09:02:24
【问题描述】:

我是这个领域的新手,甚至不是初中生1[新] 到 0[旧] 这些是我的脚本...这项工作但此查询将我所有状态为 1 的消息更改为 0,我只需要更改发件人的姓名。

这些是我的文件:这里是带有脚本 ID 的输入

echo " 
        
    <img style='display:inline-block;height:100px;width:100px;' src='images/newmessage.png'> 
   <a href='conversatie.php?catre=" . $row['nume'] . "'>
<input style='display:inline-block;font-size:15px;height:40px;  ' class='button' id='messagechangestatus' onclick='messagechangestatus()'  type='submit' value='".$row['nume']."'></a> ;

这是同一页面中的脚本:

<script type="text/javascript">
var x = document.getElementById("messagechangestatus").value;
    function messagechangestatus () {
      $.ajax({
        url:"includes/changestatus.php", 
        type: "POST", 
        data: x,
        success:function(result){
         alert(result);
       }
     });
 }
</script>

这是我的 changestatus.php 页面:

<?php 

session_start();
require "conectare.php";


$author = $_SESSION['prenume'];

   


        $sql = "SELECT * FROM messages WHERE catre='$author' AND status='1'" ;


        $result = mysqli_query($conectare, $sql);
        if(mysqli_num_rows($result) > 1){

        
            $author = $_SESSION['prenume'];   
            $sql2 = "UPDATE messages
                     SET status='0'  WHERE catre='$author'";


                     mysqli_query($conectare, $sql2);


            header("Location: mesaje.php");
            exit();

}

echo "Nu functioneaza";

?>

我想让我的 sql 查询类似于:

$sql2 = "UPDATE messages
                     SET status='0'  WHERE catre='$author' AND name =' ****And here to be the value of my input****   '";

接下来我可以做什么来在我的 sql 中获取这个输入值?

【问题讨论】:

    标签: php ajax input


    【解决方案1】:

    首先你需要修复你的 ajax 代码 因为我认为你没有发送任何内容

        <script type='text/javascript' src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script type="text/javascript">
    $(document).ready(function(){
        var x = ("#messagechangestatus").val();
              $.ajax({
                url:"includes/changestatus.php", 
                type: "POST", 
                data: { x : x },
                success:function(result){
                 alert(result);
               }
             });
    });
        </script>
    

    然后在 changestatus.php 中,您需要获取通过 ajax 在 data: { x : x } 中发送的 var x 的值,方法是添加一个如下所示的 if 语句:

    if(!isset($_POST['x'])){
    echo "your error here which shows that you're not getting the value of that x variable";}
    

    那么您可以在这里进行更新,看起来像这样:

    $messagechangestatus = $_POST['x'];
    
    $sql2 = "UPDATE messages SET status='0'  WHERE catre='$author' AND name='".$messagechangestatus."' ";
    

    【讨论】:

    • 我改变了你告诉我的方式,我把 $ 放在 var x = 之后,我将我的 changestatus.php 从包含文件夹移动到主体,因为它给了我类似“404 not found in includes/mesaje.php " 并且 mesaje.php 在我的主文件夹中......所以现在它显示了我所有的 html 页面......我无法在这里复制所有错误......它太长了
    • 确保您没有两次包含数据库,因为当您进行 ajax 调用时,这意味着您正在加载另一个页面的内容而不刷新整个页面,因此请尝试评论您的包含'是那里的数据库,看看当时发生了什么,对于 var x = ("#messagechangestatus").val();和 var x = document.getElementById("messagechangestatus").value;它几乎相同,只是第一个是 jquery 语法,第二个是 java 脚本。让我知道您的错误是否已修复。
    • 我编辑了它并使用了:var x = document.getElementById("messagechangestatus").value;而不是旧的 var x 它给了我相同的结果(在指定的警报中,甚至没有点击页面上的输入警报,我的所有 html 代码从 !DOC 到 body ...我在 mesaje.php 中有一次数据库(其中它的 ajax 但不在 ajax 中);$con=mysqli_connect("localhost","name","pass","db"); 在 messagechangestatus.php 中(我从 changestatus.php 编辑它,因为我现在有 2 个)我有这个:需要“includes/conectare.php”;对不起我的英语和那么愚蠢,但我只知道一点 php。
    【解决方案2】:

    Thx testovic,这是答案:

    在 mesaje.php 中:

       <a href='conversatie.php?catre=" . $row['nume'] . "'>
    <input style='display:inline-block;font-size:15px;height:40px;  ' class='button' id='messagechangestatus' name='messagechangestatus'  type='submit' value='".$row['nume']."'></a>
    

    这是 mesaje.php 中的脚本:

    <script type='text/javascript' src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script type="text/javascript">
    $('#messagechangestatus').on('click',function(){
        var x = document.getElementById("messagechangestatus").value;
              $.ajax({
                url:"messagechangestatus.php", 
                type: "POST", 
                data: { x : x },
                success:function(result){
               }
             });
    });
        </script>
    

    这是在 messagechangestatus.php 中:

    <?php 
    
    
    session_start();
    require "includes/conectare.php";
    
    
    $author = $_SESSION['prenume'];
    $nume = $_POST['x'];
    
            $sql = "SELECT * FROM messages WHERE catre='$author' AND status='1'";
    
    
            $result = mysqli_query($conectare, $sql);
            if(mysqli_num_rows($result) >= 1){
    
    
    if(!isset($_POST['x'])){
    echo "Nici o valoare nu a fost trecuta in input";} 
    
            else {
    
    
    
            
                $author = $_SESSION['prenume']; 
                $nume = $_POST['x'];
    
                $sql2 = "UPDATE messages SET status='0'  WHERE catre='$author' AND nume='".$nume."' ";
    
                         mysqli_query($conectare, $sql2);
    
                     }
    
                exit();
    
    }
    
    echo "Nu functioneaza";
    
    ?>
    

    再次感谢您!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      相关资源
      最近更新 更多