简体   繁体   English

如何使用ajax将参数从page1.html传递到server.php并将结果返回给page2.php?

[英]How can I pass parameters from page1.html to server.php and return results to page2.php using ajax?

I have a myfiles.php page that scans directory and displays a list of files with a radio button beside each file, and I have a load button that sends the name of the file selected to server.php which gets the data from the file and sends it back. 我有一个myfiles.php页面,它扫描目录并显示文件列表,每个文件旁边都有一个单选按钮,还有一个加载按钮,该按钮将所选文件的名称发送到server.php,该文件从文件中获取数据,发回。
My problem is I don't want the results to be sent back to myfiles.php, instead I want the result to be sent back and the browser to take me to editor.php. 我的问题是我不希望将结果发送回myfiles.php,而是希望将结果发送回并且浏览器将我带到editor.php。

Myfiles.html: Myfiles.html:

<table>
<form>
<?php 
$dir    = "./userFiles/".$login;
$files = array_diff(scandir($dir), array('..', '.')); 
foreach($files as $ind_file){ 
?> 
<tr><td><input type="radio" name="files" value="<?php echo $ind_file;?>"></td>
<td><a href="<?php echo $dir."/".$ind_file;?>"><?php echo $ind_file;?></td></tr>
<?php 
} 
?> 
</form>
</table>
<br/><button id="load">load</button>
<script>
$("#load").click( function() {
    $.ajax({
        url : server.php,
        type: 'post',
        data : {
            filename : $("input[name=files]:checked").val(),
            action : 'load'
        },
        success : function(html) {
           editor.setValue(html);
        }
    });
});

server.php: server.php:

$this->dir = "userFiles";
$this->filename = isset($_POST['filename']) ? $_POST['filename'] : false;
 private function load() {
        $content = @file_get_contents($this->dir.$this->filename);
        echo $content;
    }

Editor.php Editor.php

<textarea id="code" name="code" autofocus></textarea>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
    mode: 'text/html',
    tabMode: 'indent',
    lineNumbers: true,
    lineWrapping: true,
    autoCloseTags: true
});
</script>

Please help modify the code to solve my problem. 请帮助修改代码以解决我的问题。 Thank you. 谢谢。

First off you want to put all your [input] elements on a form. 首先,您想将所有[input]元素放在表单上。

<form name="load" action="<YOUR PHP SCRIPT>" method="post">

<?php 
$dir    = "./userFiles/".$login;
$files = array_diff(scandir($dir), array('..', '.')); 
foreach($files as $ind_file){ 
?> 
<tr><td><input type="radio" name="files" value="<?php echo $ind_file;?>"></td>
<td><a href="<?php echo $dir."/".$ind_file;?>"><?php echo $ind_file;?></td></tr>
<?php 
} 
?> 
<input type="submit" value="Load">
</form>

Server.php and Editor.php can be combined. Server.php和Editor.php可以合并。 Your PHP will look like this: 您的PHP将如下所示:

<?php
$selectedfile = $_POST['files'];
$content = file_get_contents("<YOUR DIRECTORY>" . $selectedfile);
?>

Now you can inject the $contents into your html 现在,您可以将$ contents注入到html中

<HTML>

<div><?php echo $contents; ?></div>

</HTML>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用会话将值从page1.php传递到page2.php - Passing values from page1.php to page2.php using sessions 如何同时输出仪表图和折线图实时数据,问题出在server.php页面? - How can I output gauge chart and line chart real-time data at the same time, problem is in server.php page? 如何让我的server.php不断运行? - How can I make my server.php run continually? 使用ajax将值从html传递到php页面,并在div中打印返回值 - Pass value from html to php page using ajax and print the return value in div 我可以使用php将变量从一个html页面传递到另一个html页面吗? - Can I pass a variable from one html page to another html page using php? 我无法从使用Javascript(Ajax)的PHP得到答复。 虽然我可以在PHP页面上显示结果,但我希望将其重新显示在HTML页面上 - I can't get a reply from PHP using Javascript (Ajax). While I am able to display results on PHP page, I want it back on the HTML page 如何使用AJAX将用户输入值从PHP页面传递到HTML页面 - How to pass user input value from a PHP page to HTML page using AJAX 如何在不使用JQuery或AJAX的情况下将任何类型的数据从PHP脚本页面返回到HTML页面? - How to return any kind of data from PHP Script Page to HTML Page without using JQuery or AJAX? 如何使用 AJAX 将 PHP 数组传递到 HTML 页面? - How To Pass PHP Array to HTML Page using AJAX? 我可以从php代理页面获取ajax参数吗? - can i get ajax parameters from php proxy page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM