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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效