尝试使用AJAX将变量值从JavaScript传递到PHP

时间:2021-03-07 00:06:33

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 *, but none of the them are really helping.

我想使用jQuery/AJAX将一些值从JavaScript传递到PHP。我有以下“简化”代码,不确定我做错了什么。*上似乎有很多类似的问题/答案,但没有一个是真正有用的。

HTML:

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;

?>

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.

问题是:什么都没有发生……没有错误…什么都没有。我在PHP echo语句中看不到'source1'和'source2'的值。

2 个解决方案

#1


10  

You need to include a success handler in your AJAX call:

您需要在AJAX调用中包含一个成功处理程序:

$("#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

Do make sure that both the test.php and your html source files are in same directory.

一定要确保这两个测试。php和html源文件在同一个目录中。

#2


1  

$("#text-id").click(function(e) {// because #text-id is an anchor tag so stop its default behaivour
e.preventDefault();
$.ajax({
type: "POST",// see also here
url: 'text.php',// and this path will be proper
data: {
       source1: "some text",
       source2: "some text 2"}
}).done(function( msg )
      {
       alert( "Data Saved: " + msg );// see alert is come or not
     });
});

reference ajax

参考ajax

#1


10  

You need to include a success handler in your AJAX call:

您需要在AJAX调用中包含一个成功处理程序:

$("#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

Do make sure that both the test.php and your html source files are in same directory.

一定要确保这两个测试。php和html源文件在同一个目录中。

#2


1  

$("#text-id").click(function(e) {// because #text-id is an anchor tag so stop its default behaivour
e.preventDefault();
$.ajax({
type: "POST",// see also here
url: 'text.php',// and this path will be proper
data: {
       source1: "some text",
       source2: "some text 2"}
}).done(function( msg )
      {
       alert( "Data Saved: " + msg );// see alert is come or not
     });
});

reference ajax

参考ajax