试图从JavaScript变量值传递给使用AJAX PHP [英] Trying to pass variable values from JavaScript to PHP using AJAX
问题描述
我想通过从JavaScript的一些值使用jQuery / AJAX到PHP。我有以下的简化code,不知道是我做错了。似乎是在计算器不少类似的问题/答案,但没有的他们是真正帮助。
I want to pass some values from JavaScript to PHP using jQuery/AJAX. I have the following "simplified" code, not sure what is that I am doing wrong. There seems to be quite a few similar questions/answers in StackOverflow, but none of the them are really helping.
HTML:
<div>
<a href="#" id="text-id">Send text</a>
<textarea id="source1" name="source1" rows="5" cols="20"></textarea>
<textarea id="source2" name="source2" rows="5" cols="20"></textarea>
</div>
JavaScript的:
JAVASCRIPT:
$("#text-id").click(function() {
$.ajax({
type: 'post',
url: 'text.php',
data: {source1: "some text", source2: "some text 2"}
});
});
PHP(text.php):
PHP (text.php):
<?php
$src1= $_POST['source1'];
$src2= $_POST['source2'];
echo $src1;
echo $src2;
?>
问题:什么也没有发生......不errors..nothing。我没有看到源1和源2呈现在PHP echo语句起来的值。
The problem: Nothing is happening...no errors..nothing. I don't see the values of 'source1' and 'source2' showing up in the PHP echo statements.
推荐答案
您需要包括成功处理在AJAX拨打:
You need to include a success handler in your AJAX call:
$("#text-id").on( 'click', function () {
$.ajax({
type: 'post',
url: 'text.php',
data: {
source1: "some text",
source2: "some text 2"
},
success: function( data ) {
console.log( data );
}
});
});
和在控制台,您将获得:
and in your console, you'll receive:
some textsome text 2
请务必注意两个的test.php
和你的HTML源文件在同一目录下。
Do make sure that both the test.php
and your html source files are in same directory.
这篇关于试图从JavaScript变量值传递给使用AJAX PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!