使用Ajax将变量发布到另一个PHP文件

时间:2018-05-31 10:11:09

标签: javascript php jquery ajax

嗯,这是我的问题。所以,我有一个表,使用PHP和SQL显示一些数据,如下所示:

然后,正如我在该图片中所示,我想点击该按钮,获取ID值,然后执行另一个SQL语句并在另一个PHP文件中显示另一个表。

我的问题是,正如其提到的here你真的不能使用AJAX发送它然后重定向。而且我真的是AJAX和JavaScript的新手......

当然,我知道其他方式,如<form>及其提交元素,但我已经有一个<form>,用于显示表的行,并接收新的数据输入。因此,如果我使用表单,我将不得不嵌套它们,这不起作用。

我正在尝试这个:

表格文件(我有我要发送的ID)

<script>
      function openCateg(id) {
        $.ajax({
            data: {variable: id},
            url: 'categoriasSentenciasAJAX.php',
            type: "POST",
            success: function(data) {
              alert(data);
            }
        });
        //window.location = "categoriasSentenciasAJAX.php";
      }
    </script>

categoriasSentenciasAJAX.php(我想收到数据的地方)

 $q = $_POST['variable'];

我也尝试了GET方法,只需调用:

window.location = "categoriasSentenciasAJAX.php?variable=" + id;

 $q = $_GET['variable'];

实际上,它确实有效,但是当我重新加载网站时,它并不会导致变量变为空。

我的表格(从PHP中的SQL语句生成,基本SELECT

<form method="post" onsubmit="return confirm('¿Desea actualizar los datos?')">
        <table class="w3-table w3-striped w3-bordered" id ="table" width="70%" border="1px">
          <thead>
            <tr class="w3-theme">
                <th>IDEmpresa</th>
                <th>Usuario</th>
                <th>Nombre*</th>
                <th>Direccion*</th>
                <th>Ambito*</th>
                <th>Color*</th>
                <th>Categoria</th>
            </tr>
          </thead>

          <tbody>
            <?php
            foreach ($row as $key => $row) {
                ?>
                <tr class="w3-white">
                    <td><input type="text" class="w3-input" readonly="readonly" name="idE[]" value="<?php echo $row["idEmpresa"]?>" required/></td>
                    <td><input type="text" class="w3-input" readonly="readonly" name="idU[]" value="<?php echo $row["Usuario"] ?>" required /></td>
                    <td><input type="text" class="w3-input" name="nombre[]" value="<?php echo $row["Nombre"] ?>" required /></td>
                    <td><input type="text" class="w3-input" name="direccion[]" value="<?php echo $row["Direccion"] ?>" required /></td>
                    <td><input type="text" class="w3-input" name="ambito[]" value="<?php echo $row["Ambito"] ?>" required /></td>
                    <td><input type="color" class="w3-input-color" name="color[]" value="<?php echo rgb2hex2rgb($row["Color"]) ?>" /></td>
                    <td >
                      <button id="cat" type="button" name="button[]" class="w3-btn w3-dark-grey w3-hover-light-grey" onclick="openCateg(<?php echo $row["idEmpresa"]?>);">Categorias</button>
                    </td>
                </tr>
                <?php
            }
            ?>
          </tbody>
        </table>
      </div>
    </div>
        <br>
        <div class="w3-center">
          <div class="w3-padding-32">
            <input type="submit" class="w3-btn w3-xlarge w3-dark-grey w3-hover-light-grey" value="Actualizar Empresas" name="update">
          </div>
        </div>
    </form>

所以,总而言之,我只想点击图片中显示的那个按钮,获取id并使用它将其发送到另一个文件,在那里我将调用一个SQL语句来显示另一个表。 如果可能的话,我想重定向到新的php文件。

当我按下浏览器的刷新按钮时,只想将变量发送到其他php文件而不会消失。

请原谅我的英语不好......

提前致谢!

4 个答案:

答案 0 :(得分:0)

如果刷新时值变为null,请尝试将传递的值设置为默认值。

document.getElementById("button_id").defaultValue = id;

答案 1 :(得分:0)

为什么你希望它被重定向?发布你的价值并得到回应,成功的你可以做你想做的一切。

function OpenCateg(id)
{
variable = id;
$.ajax({
type: "POST",
url: "categoriasSentenciasAJAX.php?variable="+id,
success: function(data){
window.location = "categoriasSentenciasAJAX.php";

},
error: function(data){ alert('Error occured'); }});}

答案 2 :(得分:0)

试试这个:

{{1}}

答案 3 :(得分:0)

由于您仍然会重定向到相同的 categoriasSentenciasAJAX.php 页面,因此您不需要ajax。只需使用超链接。 改变这个:

<td>
    <a href="categoriasSentenciasAJAX.php?variable=<?php echo $row["idEmpresa"] ?>" class="w3-btn w3-dark-grey w3-hover-light-grey">Categorias</a>
</td>

对此:

$q = $_GET['variable'];

使用$ _GET数组:

chr1    HAVANA  UTR 368595  368634  .   +   .   gene_id "ENSG00000235249.1"; transcript_id "ENST00000426406.1"; gene_type "protein_coding"; gene_status "KNOWN"; gene_name "OR4F29"; transcript_type "protein_coding"; transcript_status "KNOWN"; transcript_name "OR4F29-001"; level 2; tag "basic"; tag "appris_principal"; tag "CCDS"; ccdsid "CCDS41220.1"; havana_gene "OTTHUMG00000002860.1"; havana_transcript "OTTHUMT00000007999.1";
chr1    HAVANA  transcript  453633  460480  .   -   .   gene_id "ENSG00000236601.1"; transcript_id "ENST00000450983.1"; gene_type "lincRNA"; gene_status "NOVEL"; gene_name "RP4-669L17.2"; transcript_type "lincRNA"; transcript_status "KNOWN"; transcript_name "RP4-669L17.2-002"; level 2; tag "not_best_in_genome_evidence"; tag "basic"; havana_gene "OTTHUMG00000002855.2"; havana_transcript "OTTHUMT00000007988.2";

此外,您可能必须调整元素的CSS样式,以便它现在看起来像您的按钮,但我认为这不应该是很多问题=)

如果网址 categoriasSentenciasAJAX.php 变量 param的页面在重新加载时崩溃,则问题应该在此php文件中。我们在这里需要它的代码和错误消息,以便了解正在发生的事情。