简体   繁体   English

如何使用另一个PHP文件中的变量将其包含在HTML页面javascript函数中

[英]How to use variables from another php file to include in a html page javascript function

I have an HTML file, and in the file, I have a javascript validate function to validate if the input of the text field in the other PHP file is blank or not. 我有一个HTML文件,并且在文件中,我有一个JavaScript验证函数来验证另一个PHP文件中文本字段的输入是否为空。 How can I use the 'gateway' name from myFile.html. 如何使用myFile.html中的“网关”名称。 This is what i have now: 这就是我现在所拥有的:

myfile.html myfile.html

<script type="text/javascript">
    function Validate() {
            if ((document.getElementsByName("gateway")[0].value == '')) {//I need to get access to "gateway" from myOtherFile.php
                    alert('Response required');
                    return false;
            }
            else { return true; }
    }
</script>

myOtherFile.php myOtherFile.php

<div id="gatewayInput">
      <form action="/action_page.php">
           Gateway: <input type="text" name="gateway" placeholder="Gateway Name"><br>
      </form>
</div>

@kkmoslehpour there are three ways @kkmoslehpour有三种方法

make an Ajax call 拨打Ajax电话

function Validate() {

  var ajax = new XMLHttpRequest();

  ajax.onreadystatechange = function() {
      if(ajax.readyState == 4 && ajax.status == 200) {
          var value_from_gateway = ajax.responseText;
      }
  }

  ajax.send();
  ajax.open("Get", "myOtherFile.php?value_from_gateway", true);

  if ((value_from_gateway == '')) {//You have access now to "gateway" from myOtherFile.php
                alert('Response required');
                return false;
        }
        else { return true; }
}

create a redirect function on the myOtherFile.php on the top on the page -NECCESSARY 在页面顶部的myOtherFile.php上创建重定向功能-NECCESSARY

function redirect($location) {
header("Location: ". $location);

} }

then program your myOtherfile.php to work with th $_GET and send the value 然后对myOtherfile.php进行编程,使其与$ _GET一起使用并发送值

<div id="gatewayInput">
  <form action="/action_page.php" method="post">
       Gateway: <input type="text" name="gateway" placeholder="Gateway Name"><br>
  </form>
</div>
<?php

  if(isset($_GET['value_from_gateway'])) {
      $gateway_value = $_POST['gateway'];
      redirect('value_page.php?value_to_js='. $gateway_value);
  }

?>

then create the whole value_page.php to echo the value on that same page; 然后创建整个value_page.php以回显同一页面上的值;

<?php  // This is all that there should be on this page
  if(isset($_GET['value_to_js'])) {
      echo $_GET['value_to_js'];  //we are echoing this! it will be the response to the Ajax
  }
?>

The ajax call wont have any problem with this, and whatever is display on the current page (value_page.php) will be sent as the response in myfile.php Ajax调用不会对此有任何问题,并且当前页面(value_page.php)上显示的内容将作为响应发送到myfile.php中

I cannot comment but if you want to get info from your php file by your html file and javascript it could be done with ajax call to check what is inside php file and returns back with some info. 我不能发表评论,但是如果您想通过html文件和javascript从php文件中获取信息,可以用ajax调用来检查php文件中的内容并返回一些信息。

Otherwise you need to include that html file with script below your element with that name so you can use javascript to find that element and get its what you want. 否则,您需要在具有该名称的元素下方包括带有脚本的html文件,以便您可以使用javascript查找该元素并获取所需的内容。

You should put the validation code into the same file as the form, as it has to run on the client side. 由于验证代码必须在客户端运行,因此您应该将验证代码与该表单放入同一文件中。 But if for some reason you really have to have it in a separate file (and that file only contains the script tag): 但是,如果由于某种原因您确实必须将其保存在单独的文件中(并且该文件仅包含脚本标签):

echo file_get_contents('myfile.html');

It is now magically in this file as well. 现在它也神奇地存在于该文件中。 But seriously just put it in the same file. 但是认真地把它放在同一个文件中。

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

相关问题 如何使用JavaScript中另一个文件中的变量和函数? - How do I use variables and function from another file in javascript? 如何在另一个类似php include()函数的HTML文件中导入HTML - How import html in another html file like php include() function 如何在另一个页面 html 中包含 html 和 javascript? - How to include html and javascript in another page html? 将变量从javascript文件传递到另一个html页面 - passing variables from a javascript file to another html page 如何在另一个JavaScript文件的功能内使用远程存储的JavaScript文件中的变量? - How could I use the variables from a remotely stored JavaScript file within the function of another JavaScript file? 不确定如何在 HTML 文件中使用 PHP function 文件,该文件利用 Z4C4AD5FCAED007A3F7A4DBB1 中的 JS 变量 - Not sure how to use PHP function in HTML file that leverages JS variables from that HTML file 包含其他文件中的javascript和html代码 - Include javascript and html code from another file 如何从一个页面获取JavaScript并在另一页面的php中使用它 - How to take javascript from one page and use it in php on another page 如何使用JavaScript中另一个函数的变量 - How can I use variables from another function in javascript 如何将文件类型输入传递给 javascript 并将其用于另一个 php 页面? - How to pass a file type input into javascript and use it into another php page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM