使用ajax向php文件传递变量无效

时间:2021-06-03 21:31:58

I am trying to pass a variable when a class("women") is clicked using ajax to a php file but it is not working. Here is my code

当使用ajax单击一个类(“women”)时,我试图将一个变量传递给php文件,但它不能工作。这是我的代码

jquery:

jquery:

$('.women').click(function(){
    var test="hello";
    $.ajax({
    type: "POST",
    url: 'data.php',
    data: {'variable':test},
        success:function(data){
            console.log(data);
        },
    });
     $(".women").attr('href','data.php');
})

php code:

php代码:

if (isset($_POST['variable']))
{
    echo($_POST['variable']);
}
else
{
   echo ("failure");
}

html:

html:

<li class="nav-item mr-auto ml-auto" data-target="#collapsewomen">
    <a class="nav-link active women productlink"  href="#">Women</a>
</li>

In the console I can see "hello" which mean ajax is working, but once directed to php page I get "failure". What I am not able to pass test variable to php file

在控制台,我可以看到“hello”,这意味着ajax正在工作,但是一旦指向php页面,就会出现“失败”。我不能把测试变量传递给php文件

4 个解决方案

#1


2  

The purpose of ajax is to send data to a URL without refreshing the page. If you want redirect the page, there is no use of using ajax.

ajax的目的是在不刷新页面的情况下将数据发送到URL。如果您想重定向页面,使用ajax是没有用的。

Doing an ajax call will not automatically save the data sent and the data can't be use if you redirect to that page.

执行ajax调用不会自动保存发送的数据,如果重定向到该页,则不能使用数据。

Using GET

使用GET

$('.women').click(function(){
     var test="hello";
     window.location.href = "data.php?variable=" + test;
})

On your php

在php

if (isset($_GET['variable']))
{
    echo($_GET['variable']);
}
else
{
   echo ("failure");
}

Using POST, one option is to use hidden form like:

使用POST,一种选择是使用隐藏形式:

On your main page:

在你的主页:

$('.women').click(function(){
    var test = "hello";
    $('[name="variable"]').val(test);  //Update the value of hidden input
    $("#toPost").submit();             //Submit the form
})

HTML:

HTML:

<a class="nav-link active women productlink"  href="#">Women</a>

<form id="toPost" action="data.php" method="POST">
  <input type="hidden" name="variable" value="">
</form>

On your data.php:

在你data.php:

<?php 
if (isset($_POST['variable']))
{
    echo($_POST['variable']);
}
else
{
   echo ("failure");
}

?>

#2


1  

I think it should be data: {variable:test} not data: {'variable':test}

我认为应该是data: {variable:test} not data: {'variable':test}

#3


0  

add this to your frontend file

将此添加到前端文件

<input type="button" class="women" value="Button"  name="ash_b" />

<script type="text/javascript"  src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js" >


$('.women').click(function(){
var test="hello";
$.ajax({
type: "POST",
url: 'data.php',
data: {'variable':test},
    success:function(data){
        console.log(data);
    },
  });
  $(".women").attr('href','data.php');
 })
</script>

And in your data.php use following code

和在你的数据。php使用代码

if (isset($_POST['variable'])) 
 {
   echo($_POST['variable']);
 }
 else
 {
   echo ("failure");
 }

it works perfectly for me, try this if this doesnt work show your both files

这对我来说是完美的,如果不行,试试这个

#4


0  

Please try this code:

请试试这段代码:

$('.women').click(function(){
var test="hello";
var datastr = 'test='+test; 
$.ajax({
type: "POST",
url: 'data.php',
data: datastr,
    success:function(data){
        console.log(data);
    },
  });
  $(".women").attr('href','data.php');
 })

#1


2  

The purpose of ajax is to send data to a URL without refreshing the page. If you want redirect the page, there is no use of using ajax.

ajax的目的是在不刷新页面的情况下将数据发送到URL。如果您想重定向页面,使用ajax是没有用的。

Doing an ajax call will not automatically save the data sent and the data can't be use if you redirect to that page.

执行ajax调用不会自动保存发送的数据,如果重定向到该页,则不能使用数据。

Using GET

使用GET

$('.women').click(function(){
     var test="hello";
     window.location.href = "data.php?variable=" + test;
})

On your php

在php

if (isset($_GET['variable']))
{
    echo($_GET['variable']);
}
else
{
   echo ("failure");
}

Using POST, one option is to use hidden form like:

使用POST,一种选择是使用隐藏形式:

On your main page:

在你的主页:

$('.women').click(function(){
    var test = "hello";
    $('[name="variable"]').val(test);  //Update the value of hidden input
    $("#toPost").submit();             //Submit the form
})

HTML:

HTML:

<a class="nav-link active women productlink"  href="#">Women</a>

<form id="toPost" action="data.php" method="POST">
  <input type="hidden" name="variable" value="">
</form>

On your data.php:

在你data.php:

<?php 
if (isset($_POST['variable']))
{
    echo($_POST['variable']);
}
else
{
   echo ("failure");
}

?>

#2


1  

I think it should be data: {variable:test} not data: {'variable':test}

我认为应该是data: {variable:test} not data: {'variable':test}

#3


0  

add this to your frontend file

将此添加到前端文件

<input type="button" class="women" value="Button"  name="ash_b" />

<script type="text/javascript"  src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js" >


$('.women').click(function(){
var test="hello";
$.ajax({
type: "POST",
url: 'data.php',
data: {'variable':test},
    success:function(data){
        console.log(data);
    },
  });
  $(".women").attr('href','data.php');
 })
</script>

And in your data.php use following code

和在你的数据。php使用代码

if (isset($_POST['variable'])) 
 {
   echo($_POST['variable']);
 }
 else
 {
   echo ("failure");
 }

it works perfectly for me, try this if this doesnt work show your both files

这对我来说是完美的,如果不行,试试这个

#4


0  

Please try this code:

请试试这段代码:

$('.women').click(function(){
var test="hello";
var datastr = 'test='+test; 
$.ajax({
type: "POST",
url: 'data.php',
data: datastr,
    success:function(data){
        console.log(data);
    },
  });
  $(".women").attr('href','data.php');
 })