dove2199 2016-02-28 14:43
浏览 114
已采纳

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

I would like to use the 'sID' in the first HTML form to retrieve data from the database and then use the data retrieved from the database on the second HTML page. I can do this with just php, but I just can't figure out how to do it using ajax.

I'm really new to javascript/ajax so please be gentle with your answers :)

HTML 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);}

    });

HTML 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> 

What i'm trying to do is; start from HTML 1, collect sID -> then PHP/JS use sID from HTML 1 to get data from database -> then use the result from database on HTML 2. At the moment i'm struggling on how to make this process work. Can't figure out how to start from HTML 1 and end up in HTML 2.

  • 写回答

2条回答 默认 最新

  • douyoufan7881 2016-02-28 16:03
    关注

    You are not fetching the data from the input element at all.. change your ajax code to below.

    $.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
    

    Edit 1: you can use localstorage to save data and retrieve from there when ever required. So you can do as below

    In your HTML 1 write this.

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

    And in HTML 2 you can access the value by reading it from the local storage like below,

    var sIDofHTML1 = JSON.parse(localStorage.getItem('sID'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办