从一个 php 文件,我需要将两个 ajax 变量传递给两个处于相同 isset 条件的独立 javascript 文件



在php文件中:

if(isset($_POST["my_first_variable"]))
{
if(empty($_POST["my_first_variable"]))
{
//This $rtrn variable I need to return to another ajax function in 
another javascript file
$rtrn["my_return_second_variable"]="First variable is empty";
echo json_encode($rtrn);
}
}

从第一个javascript文件开始,我将输入数据变量发送到php,在那里我检查该值是否为空且不正确,如果是,我需要将返回的数据发送到另一个javascript文件,该文件告诉输入为空或不正确以禁用主页上的提交按钮。

PHP 不能任意将数据发送到某个随机文件。它将数据发送回请求它的文件。

你需要你的Javascript来相互通信:

我的脚本.php

if(isset($_POST["my_first_variable"]))
{
if(empty($_POST["my_first_variable"]))
{
//This $rtrn variable I need to return to another ajax function in 
another javascript file
$rtrn["my_return_second_variable"]="First variable is empty";
echo json_encode($rtrn);
}
}

第一.js

$(function(){
$.ajax({
url: 'www.example.com/myscript.php', // Send a request with POST data to this file
type: 'POST', // Send as a POST and not GET
data: { 'my_first_variable' : '' }, // Make sure this data is set but empty to satisfy the logic in myscript.php
dataType: 'json', // We expect to receive JSON data
success: function( data ){
doSomething( data ); // Send this data to second.js
}
});
});

第二.js

function doSomething( incomingData ){
alert( incomingData[ 'my_return_second_variable' ] );
}

确保加载这两个文件,它应该可以工作。


为了任何使用谷歌的人,一些常见的搜索短语可能是:

  • 如何将数据从一个JS文件发送到另一个JS文件?
  • PHP如何拆分哪些JS文件接收数据?
  • 将数据从一个 Js 文件传递到另一个 Js 文件。

最新更新