如何使用一个 HTML 页面中的数据来检索要在另一个 HTML 页面上使用 ajax 的数据


How to use data from one HTML page to retrieve data to be used on another HTML page using ajax

我想在第一个HTML表单中使用'sID'从数据库中检索数据,然后在第二个HTML页面上使用从数据库中检索的数据。我可以用 php 做到这一点,但我只是不知道如何使用 ajax 做到这一点。

我真的是javascript/ajax的新手,所以请温和地回答你的答案:)

网页 1

            <div class="moreR">             
                 <form action="moreR_2.0.php" method="GET">
                    <input type="hidden" name="sID[]" value="a_certain_ID"/>
                   <input type="image" src="Icons/PNG/greater_than.png" alt="submit"/>
                </form>
            </div>

PHP (moreR_2.0.php)

<?php
        include ('session_start.php');        
        include ('db_connect_mO.php');
    if (isset($_GET['sID'])) {
        foreach($_GET['sID'] as $sID) { 
            }
}
    $sql = mysqli_query($con, "SELECT * FROM mo WHERE sID=$sID");
    $row = mysqli_fetch_array($sql);
        while ($row = mysqli_fetch_assoc($sql)) 
        {           
            $test[]= array( 
                            'pZero'=> $row['pZero'],
                            'pZero_Gname'=> $row['gZero_key'],  
                        );
        }
        header('Content-Type: application/json');
        echo json_encode ($test);
        //detailed error reporting
        if (!$sql) 
        {
            echo 'MySQL Error: ' . mysqli_error($db);
        exit;
        }
?>

JavaScript

$(document).ready(function() {
    "use strict"; 
    function connect2mR() { 
    $.ajax({
        url:"moreR_2.0.php",
        type: "GET",
        data:'sID',
        dataType:"json",
        //async:false,
        success:function(data)
        {    
            $('#pZero').html('<img src="rPlanets/' + this.gZero + '.png" alt=""/>');
            $('#pZero_keys').html(this.gZero_key);
        }, //success

    }); //end of ajax
    } //end of function
    if (window.attachEvent) {window.attachEvent('onload', connect2mR);}
    else if (window.addEventListener) {window.addEventListener('load',     connect2mR, false);}
    else {document.addEventListener('load', connect2mR, false);}
    });

网页 2

<section class="moreR_section">
    <div style="width:20%;"><div id="pZero"></div></div>
    <div class="moreR_g" style="margin-left:26%" id="pZero_keys"></div>
</section> 

我想做的是;从HTML 1开始,收集sID ->然后PHP/JS使用HTML 1中的sID从数据库获取数据>然后在HTML 2上使用数据库的结果。目前,我正在努力使这个过程发挥作用。无法弄清楚如何从 HTML 1 开始并以 HTML 2 结束。

你根本没有从输入元素获取数据......将你的ajax代码更改为下面。

$.ajax({
    url:"moreR_2.0.php",
    type: "GET",
    data:{sID: $('input[name="sID[]"]').val()},  // this is the change
    dataType:"json",
    //async:false,
    success:function(data)
    {    
        $('#pZero').html('<img src="rPlanets/' + this.gZero + '.png" alt=""/>');
        $('#pZero_keys').html(this.gZero_key);
    }, //success

}); //end of ajax

编辑1:您可以使用localstorage保存数据并在需要时从那里检索。所以你可以做如下

在你的HTML 1中写这个。

localStorage.setItem('sID', JSON.stringify( $('input[name="sID[]"]').val()));

在 HTML 2 中,您可以通过从本地存储中读取值来访问该值,如下所示,

var sIDofHTML1 = JSON.parse(localStorage.getItem('sID'));

您必须按如下方式更新ajax

data:'sID',  // this has to change to data:'sID='+sID,

$.ajax({
    url:"moreR_2.0.php",
    type: "GET",
    data:'sID',  // this has to change to data:'sID='+sID,
    dataType:"json",
    //async:false,
    success:function(data)
    {    
        $('#pZero').html('<img src="rPlanets/' + this.gZero + '.png" alt=""/>');
        $('#pZero_keys').html(this.gZero_key);
    }, //success

}); //end of ajax