首页 > 解决方案 > 将值从服务器返回到 html 以进行操作

问题描述

我想操纵它存储在这个变量中的值$result[]。具体来说,我想将该值从 php 文件返回到我的 html 文件。我该怎么办?当我想将东西从服务器端返回到客户端以进行进一步操作时,你能给我一些参考代码吗?

我的 php 文件:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];

//Attempt insert query execution
$query = "SELECT site_id FROM components WHERE user_id='$user_id' && book_id='$book_id' && game_id='$game_id' ORDER BY site_id DESC LIMIT 1";
$res = mysqli_query($link,$query);
$result = array();
$res = mysqli_query($link,$query) or die(mysqli_error($link));
while($row = mysqli_fetch_assoc($res)){
  $result[]=$row['site_id'];
}

// Close connection
mysqli_close($link);
?>

我的html文件:

    <html>

    <head>
      <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
      <script>
        function load3() {
          var flag1 = true;
          do{
            var selection = window.prompt("Give the User Id:", "Type a number!");
                if ( /^[0-9]+$/.test(selection)) {
                flag1=false;
                }
            }while(flag1!=false);
        $("#user_id").val(selection)
          //$("#user_id").val(prompt("Give the User Id:"))

          var flag2 = true;
          do{
            var selection2 = window.prompt("Give the Book Id:", "Type a number!");
            if ( /^[0-9]+$/.test(selection2)) {
                flag2=false;
                }
            }while(flag2!=false);
        $("#book_id").val(selection2)
          //$("#book_id").val(prompt("Give the Book Id:"))

          var flag3= true;
          do{
            var selection3 = window.prompt("Give the Game Id:", "Type a number!");
            if ( /^[0-9]+$/.test(selection3)) {
                flag3=false;
                }
            }while(flag3!=false);
        $("#game_id").val(selection3)
          //$("#game_id").val(prompt("Give the Game Id:"))
       }    
var fieldNameElement = document.getElementById('outPut');
 if (fieldNameElement == 4)
 {
 window.alert("bingo!");
 }
      </script>

    </head>

    <body>
                      <form name="LoadGame" id="LoadGame" method="post" action="http://127.0.0.1/PHP/mine1.php" enctype="multipart/form-data">
                        <input type="submit" value="Load" id="load" onclick="load3()" class="button12" />
                        <input type="hidden" name="user_id" id="user_id">
                        <input type="hidden" name="book_id" id="book_id">
                        <input type="hidden" name="game_id" id="game_id">
                        <input type="hidden" name="site_id" id="site_id">
                      </form>     
    </body>
    </html>

标签: javascriptphpjqueryajax

解决方案


首先:从你的 php.ini 中删除脚本标签。其次:为什么要执行两次sql语句?

对于您的问题:您必须通过 AJAX 向您的 PHP 脚本发送请求:(将其放在<script>标签内并确保正确包含 jquery)

$(() => {

  $('form').on('submit', () => {

    event.preventDefault()

    $.ajax({
      type: 'POST',
      url: '<your-php-file>', // Modify to your requirements
      dataType: 'json', 
      data: $('form').serialize() // Modify to your requirements
    }).done(function(response){

      console.log(response)

    }).fail(function(){

      console.log('ERROR')

    })

  })

})

您需要返回 JSON 的 PHP 脚本:

$query = "SELECT site_id FROM components WHERE user_id='$user_id' && book_id='$book_id' && game_id='$game_id' ORDER BY site_id DESC LIMIT 1";

// Execute Query
$res = mysqli_query($link,$query) or die(mysqli_error($link));

// Get Rows
while($row = mysqli_fetch_assoc($res)){
  $result[] = $row['site_id'];
}    

// Return JSON to AJAX
echo json_encode($result);

看看你的开发者控制台。

没有测试过。


推荐阅读