通过 ajax 函数将字符串传递到它被回显的页面,PHP、AJAX、JAVASCRIPT
Passing string through ajax function to the page where it gets echoed, PHP, AJAX, JAVASCRIPT
我有三个文件。 test1.php、test2.php 和 ajax.php。在 test1.php 中,我有一个文本输入。当我在文本输入中键入内容时,它会调用 ajax 函数,并将我在文本输入中键入的内容作为 POST 传递给 test2.php。在 text2.php 中回显后,它显示在 test1.php 中输入的文本下方。
现在,我想通过 ajax 函数 将字符串传递给 test2.php,然后再回显它,因此它会显示在test1.php 中的文本输入。例如,我想通过 ajax 函数将单词 "Hello" 传递给 test2.php - 我将如何做到这一点,或者这是否可能?谢谢你。
test1.php
<?php
include('ajax.php');
echo "<input type = 'text' name = 'text' onkeyup = 'ajax(\"test2.php\",\"input\",\"text\",\"output\")'>";
echo "<div id = 'output'/>";
?>
test2.php
<?php
$text = $_POST['text'];
echo $text;
?>
ajax.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function ajax(gotoUrl,type,inputName,output) {
$.ajax({
type: "POST",
url: gotoUrl,
data: { select: $(type+'[name='+inputName+']').val()},
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById( output ).innerHTML = data;
}
});
}
</script>
很有可能。
在 ajax.php: 添加一个新元素到你的 data:{}
数组:
data: { select: $(type+'[name='+inputName+']').val(), some_string: 'Hello' },
In test2.php: 您现在可以引用从您的 ajax 请求发送的第二个元素:
<?php
$text = $_POST['select'];
$some_string = $_POST['some_string'];
echo $some_string.$text;
?>
我有三个文件。 test1.php、test2.php 和 ajax.php。在 test1.php 中,我有一个文本输入。当我在文本输入中键入内容时,它会调用 ajax 函数,并将我在文本输入中键入的内容作为 POST 传递给 test2.php。在 text2.php 中回显后,它显示在 test1.php 中输入的文本下方。
现在,我想通过 ajax 函数 将字符串传递给 test2.php,然后再回显它,因此它会显示在test1.php 中的文本输入。例如,我想通过 ajax 函数将单词 "Hello" 传递给 test2.php - 我将如何做到这一点,或者这是否可能?谢谢你。
test1.php
<?php
include('ajax.php');
echo "<input type = 'text' name = 'text' onkeyup = 'ajax(\"test2.php\",\"input\",\"text\",\"output\")'>";
echo "<div id = 'output'/>";
?>
test2.php
<?php
$text = $_POST['text'];
echo $text;
?>
ajax.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function ajax(gotoUrl,type,inputName,output) {
$.ajax({
type: "POST",
url: gotoUrl,
data: { select: $(type+'[name='+inputName+']').val()},
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById( output ).innerHTML = data;
}
});
}
</script>
很有可能。
在 ajax.php: 添加一个新元素到你的 data:{}
数组:
data: { select: $(type+'[name='+inputName+']').val(), some_string: 'Hello' },
In test2.php: 您现在可以引用从您的 ajax 请求发送的第二个元素:
<?php
$text = $_POST['select'];
$some_string = $_POST['some_string'];
echo $some_string.$text;
?>