简体   繁体   English

如何将变量从 javascript 发送到 PHP

[英]How to send variables from javascript to PHP

I want to know how to send variables from javascript to php so i can create a variable that contains dynamic sum of rows.我想知道如何将变量从 javascript 发送到 php,以便我可以创建一个包含动态行总和的变量。

More specific: When I search in my search box, i want to get the number of rows (1 match is 1 row, 2 matches is 2 rows and so on更具体:当我在搜索框中搜索时,我想获取行数(1 个匹配是 1 行,2 个匹配是 2 行等等

I tried to implement this: document.getElementById("i1").value = allCells.length;我试图实现这个: document.getElementById("i1").value = allCells.length; so i later could call in the php, but i did not work.所以我后来可以调用 php,但我没有工作。

This is my javascript, by the way the javascript works perfectly.这是我的 javascript,顺便说一下 javascript 完美运行。

<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
<script language="javascript" type="text/javascript">  
  $(document).ready(function() 
  {
    $('#search').keyup(function() {
      searchTable($(this).val());
    });
  });
  function searchTable(inputVal) 
  {
    var table = $('.table');
    table.find('tr').each(function(index, row) 
    {
      var allCells = $(row).find('td');
      if (allCells.length > 0) {
        var found = false;
        allCells.each(function(index, td) 
        {
          var regExp = new RegExp(inputVal, 'i');
          if (regExp.test($(td).text())) 
          {
            found = true;
            return false;
            document.getElementById("i1").value = allCells.length;
          }
        });
        if (found == true)
          $(row).show();
        else
          $(row).hide();
      }
    });
  }

  $(function()
  {
    $('#table a').click(function(e) 
    {
      e.preventDefault();
      $('#result').val($(this).closest('tr').find('td:first').text());
    });
  });
</script>

I wanted to spit the dynamicelly sum of rows her in my table header.我想在我的表头中动态地吐出行的总和。

 <h3>Total: (<?php print_r($_GET["i1"])?>)  </h3>

I hope you can help me.我希望你能帮助我。

You probably have never learned the difference between javascript and php您可能从未了解过 javascript 和 php 之间的区别

Javascript is clientsided, which means everything is processed by your local system. Javascript 是客户端的,这意味着一切都由您的本地系统处理。 PHP is server sided which means everything is processed by the server and parsed into html. PHP 是服务器端的,这意味着所有内容都由服务器处理并解析为 html。

You can't send a value from javascript into plain php like you did.您不能像以前那样将值从 javascript 发送到普通的 php 中。 You can however send a post or get to the same script and let that reload a part of your script但是,您可以发送帖子或访问相同的脚本,然后重新加载脚本的一部分

http://api.jquery.com/jQuery.get/ http://api.jquery.com/jQuery.get/

http://api.jquery.com/jQuery.post/ http://api.jquery.com/jQuery.post/

You're not the first to want this, and not the first to be told it is impossible the way you imagine.您不是第一个想要这个的人,也不是第一个被告知这是不可能的。 When you browse to a PHP page things go basically like this:当你浏览到一个 PHP 页面时,事情基本上是这样的:

  1. Browser sends HTTP request to server浏览器向服务器发送 HTTP 请求
  2. Server determines what page to send to the browser服务器决定向浏览器发送什么页面
  3. Server discovers you want a PHP page服务器发现你想要一个 PHP 页面
  4. Server executes PHP服务器执行 PHP
  5. Server sends what is returned by PHP to the browser服务器将 PHP 返回的内容发送给浏览器
  6. Browser doesn't know about the PHP and displays HTML浏览器不知道 PHP 并显示 HTML
  7. Browser executes Javascript.浏览器执行 Javascript。

Now the important part is that the browser doesn't know what PHP is, but can execute JavaScript, while the server doesn't know what JavaScript is (for simplicity's sake) but can execute PHP.现在重要的是浏览器不知道PHP是什么,但可以执行JavaScript,而服务器不知道JavaScript是什么(为简单起见)但可以执行PHP。 Bottomline: it is hard to communicate between the two because they are executed in different places.底线:两者之间很难沟通,因为它们是在不同的地方执行的。 You'll most probably want to use AJAX, so here is a supersimple sample:您很可能想使用 AJAX,所以这里是一个超级简单的示例:

The PHP page we're going to fetch:我们要获取的 PHP 页面:

<?
    // We're on the server
    // We're going to output something:
    echo "Yay! You can see me!"; // So the browser sees only "Yay! You can see me!" (without quotes).
?>

JavaScript (with jQuery) to fetch the PHP page:用于获取 PHP 页面的 JavaScript(使用 jQuery):

$("#anElementID").load("thePHPPageWeWantToFetch.php"); // It's that simple! The element with the ID #anElementID now contains "Yay! You can see me!" (without quotes).

I suggest too, use AJAX or something to pass your javascript values to PHP.我也建议,使用 AJAX 或其他东西将您的 javascript 值传递给 PHP。 You can create a AJAX call, add the data from your javascript, and catch the data in your PHP file.您可以创建一个 AJAX 调用,从您的 javascript 添加数据,并在您的 PHP 文件中捕获数据。

var value = "Jan";
$.ajax({
        url: "/form.php",
        type: "post",
        data: "name=" + value
    });

in your PHP file you can do:在您的 PHP 文件中,您可以执行以下操作:

<?php
$catchedName = $_POST["name"];
?>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM