douzhuo6270 2015-05-04 03:18
浏览 54

如何从PHP向Javascript发送变量?

How do I send variable value from PHP to Javascript using Ajax? Below is the JS code and PHP code

PHP CODE:

<?php
    $username = "trainerapp";
    $password = "password";
    $hostname = "localhost";
    $link = @mysql_connect($hostname, $username, $password);

    if(@mysql_select_db("trainer_registration"))
    {
        echo "Connected Successfully";
    }
    else
    {
        echo "Connection Error";
    }



    $select_query_num = @mysql_query("select id,program_name,company,date_prog from program_details");
    $num_rows = @mysql_num_rows($select_query_num);
    for ($i = 0; $i <= $num_rows; $i++){
            $id = $_POST["idjs"];
            $pgmname = $_POST["pgmnamejs"];
            $comp = $_POST["compjs"];
            $datephp = $_POST["datephpjs"];
        $select_query = @mysql_query("select id,program_name,company,date_prog from program_details where id = $i");
        $fetch_query = @mysql_fetch_assoc($select_query);
        $id =  $fetch_query['id'];
        echo $id;
        $pgmname = $fetch_query['program_name'];
        echo $pgmname;
        $comp =  $fetch_query['company'];
        echo $comp;
        $datephp = $fetch_query['date_prog'];
        echo $datephp;  

        }
?>

JS CODE:

window.onload = function createdivs() {
        var id;
        var pgmname;
        var comp;
        var datephp;
        var i = 1;
        for (;i < 10;i++)
        {
            div = "<div>.display";
            var list = document.createElement("div");
            document.getElementById('fulldisplay').appendChild(list);
            list.className = "container content-rows";
        }
        $.ajax({
        url:'displaycontent.php',
        data:{idjs:id, pgmnamejs:pgmname, compjs:comp, datephpjs:datephp},
        type:'POST',
        success:function(retval){
        alert(retval);
        }
    });

    }

Questions:

In JS CODE, for every increment of var i, I need to make a Ajax call to the PHP file which should return the first array of values and second and so on. I'm actually confused of how to do this. An explanation would be better. By the above code I get only the last array value with an unidentified index error.

  • 写回答

3条回答 默认 最新

  • dty3416 2015-05-04 04:01
    关注

    If this is not a formal work then just echo the HTML in the php script.

    $select_query = @mysql_query("select id,program_name,company,date_prog from program_details where id = $i");
    $fetch_query = @mysql_fetch_array($select_query. MYSQL_NUM);
    list($id, $pgname, $comp, $datephp) = $fetch_query;
    echo "
      <div>
        $id $pgname $comp $datephp
      </div>
    "; 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题