Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用XMLHTTP将变量从JavaScript传递到PHP页面_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

如何使用XMLHTTP将变量从JavaScript传递到PHP页面

如何使用XMLHTTP将变量从JavaScript传递到PHP页面,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,请先阅读说明,然后再将其标记为副本 据我所知,将数据从JavaScript传递到PHP的方法是通过Ajax调用 我的场景是这样的: 使用PHP,我回显了一个包含JavaScript的HTML页面 <?php $out .= ' <h3>Upload New Primary Studies</h3> <div> <p> Please select the primary studies which you

请先阅读说明,然后再将其标记为副本

据我所知,将数据从JavaScript传递到PHP的方法是通过Ajax调用

我的场景是这样的:

  • 使用PHP,我回显了一个包含JavaScript的HTML页面

    <?php        
    $out .= '    
    <h3>Upload New Primary Studies</h3>    
    <div>    
    <p> Please select the primary studies which you want to upload:</p>    
    <table>    
    <form id="file-form" action="" method="POST">
    <Tr><input type="file" id="file-select" name="userFile" multiple/>                 
    <button type="submit" id="upload-button"">Upload</button>                 
    </Tr>               
    </form>      
    </table>        
    </div>
    
    <h3>View / Edit Primary Studies</h3>
    <div id="grid1"></div>
    <script>
    
    var form = document.getElementById("file-form");
    var fileSelect = document.getElementById("file-select");
    var uploadButton = document.getElementById("upload-button");
    var today = new Date();
    var idValue = 1;      
    var jsonData = {
    
        header: []
    
    };
    
    var data1;
    
    form.onsubmit = function(event) {
      event.preventDefault();
    
    // Update button text.
    uploadButton.innerHTML = "Uploading...";
    
    
    // Get the selected files from the input.
    var files = fileSelect.files;
    var file1 = files[0];
    
    var i = 1;
    var reader = new FileReader();
    reader.readAsText(file1);
    reader.onload = function(event){
        var csv = event.target.result;
        var data = $.csv.toArrays(csv);
        for(var row in data) {
            if ( row == 0)
                continue;
            else
            {
                jsonData.header.push({
    
                    "ID" : idValue,
                    "RefID" : 1,
                    "RefType" : data[row][0],
                    "recordDate" : new Date().toJSON().slice(0,10),
                    "PrimaryAuthors" : data[row][1],
                    "PrimaryTitle" : data[row][2],
                    "FullPeriodical" : data[row][3],
                    "PeriodicalAbbrev" : data[row][4],
                    "PubYear" : data[row][5],
                    "PubDate" : data[row][6],
                    "Volume" : data[row][7],
                    "Issue" : data[row][8],
                    "StartPage" : data[row][9],
                    "OtherPage" : data[row][10],
                    "Keywords" : data[row][11],
                    "Abstract" : data[row][12],
                    "Notes" : data[row][13],
                    "PersonalNotes" : data[row][14],
                    "SecondaryAuthors" : data[row][15],
                    "SecondaryTitle" : data[row][16],
                    "Edition" : data[row][17],
                    "Publisher" : data[row][18],
                    "PlacePub" : data[row][19],
                    "TertiaryAuthors" : data[row][20],
                    "QuaternaryAuthors" : data[row][21],
                    "QuinaryAuthors" : data[row][22],
                    "TertiaryTitle" : data[row][23],
                    "ISSN" : data[row][24],
                    "Availability" : data[row][25],
                    "Address" : data[row][26],
                    "AccNumber" : data[row][27],
                    "Language" : data[row][28],
                    "Classification" : data[row][29],
                    "SubFile" : data[row][30],
                    "OrgForiegnTitle" : data[row][31],
                    "url" : data[row][32],
                    "DOI" : data[row][33],
                    "CallNumber" : data[row][34],
                    "Database" : data[row][35],
                    "DataSource" : data[row][36],
                    "IdentPhrase" : data[row][37],
                    "RetDate" : data[row][38]
                });
                idValue++;
            }
        }
            data1 = jsonData;
            //alert(JSON.stringify(data1));
            $("#grid1").igGrid("dataSourceObject", data1);
            $("#grid1").igGrid("dataBind");
    
        }
    
    
        uploadButton.innerHTML = "Upload Complete";
    }
    
    // Call the grid with JSON Data
    $( document ).ready(grid1());
    var request;
    function createDBRows()
    {
        var r = confirm("Do you want to create records in the database");
    
        var url = "/_application/model/dataAccess/insertStudies.php?json=";
        if (r == true) {
    
            alert("You pressed OK!");
            // Get the database of the Grid
            var dbSource = $("#grid1").igGrid().data().igGrid.dataSourceObject();
    
    
    
        } else {
           alert("You pressed Cancel!");
        }
    }
    </script>
    <br>
    <br>
    
    <div>
    
    <button type="submit" id="upload-button"" onclick="createDBRows()">Create  Database Rows</button>
    
    <button type="clear" id="clear-button"">Clear</button>    
    <button type="cancel" id="cancel-button"">Cancel</button>
    </div>
    ';
    

    你要的是一个

    您可以通过json数组或表单序列化将数据发送到php页面

    该函数将数据从php页面返回到带有一类结果的标记中

    $.post("path/yourPhpPage.php",{key: 'value'}, function( data ) {
      $( ".result" ).html( data );
    });
    

    点击按钮运行这个脚本(它进一步将3个JS变量传递给abc.php

    然后在abc.php//文件中

    <?php
        $recieved_1 = $_GET['recieve_1'];
        $recieved_2 = $_GET['recieve_2'];
        $recieved_3 = $_GET['recieve_3'];
    
        //Do your processing here and insert it in the database
            //Insert Query OR Update Query (whatever)
        // after you have inserted you can get the response in
        // the <div> having id="myDiv" Or whatever you want
        // Suppose you have successfully inserted data then 
        $insertedDataBoolean = true;
        if($insertedDataBoolean){
            echo "Data: " . $recieved_1 . ", " . $recieved_2 . 
            " and " . $recieved_3 . " inserted successfully.";
        }
        else{
            echo "Data-insertion error.";
        }
    ?>
    
    
    
    您是否在任何地方包含jquery库?@demoave yes它包含在主标题页中。@D4V1D我读过那篇文章,但它没有回答我在PHP页面a中使用Ajax将数据传递到PHP页面B的问题。奇怪的是,为什么要将整个html放在一个字符串中?您的grid1()fn在哪里?如果您正在使用jquery,为什么不在jquery中编写整个js呢?这是老方法,我们现在有了库。他要求jquery,我在这里一步一步地解释它。谢谢你的回复。我以前试过上述方法,但不起作用。当我尝试在我的程序中使用类似的代码行时,它不接受$.post作为有效代码,因为我已经在使用PHP并回显html+javascript代码。使用PHP不会干扰javascript,因为PHP是服务器端,javascript是客户端。您可能面临的唯一问题是单引号/双引号错误。此外,$post方法必须位于ready中
    <body>
        <div id="myDiv" style="border: 1px solid BLUE; height: 100px;">
            the things echo'ed in abc.php will come in this div
        </div>
        <button onclick="sendJSValsToPHP();">Click Me To Send JS-Values to abc.php</button>
    </body>
    
    <?php
        $recieved_1 = $_GET['recieve_1'];
        $recieved_2 = $_GET['recieve_2'];
        $recieved_3 = $_GET['recieve_3'];
    
        //Do your processing here and insert it in the database
            //Insert Query OR Update Query (whatever)
        // after you have inserted you can get the response in
        // the <div> having id="myDiv" Or whatever you want
        // Suppose you have successfully inserted data then 
        $insertedDataBoolean = true;
        if($insertedDataBoolean){
            echo "Data: " . $recieved_1 . ", " . $recieved_2 . 
            " and " . $recieved_3 . " inserted successfully.";
        }
        else{
            echo "Data-insertion error.";
        }
    ?>