简体   繁体   English

在同一页面上使用ajax将js变量传递给php

[英]pass js variable to php using ajax on the same page

this is my html code: 这是我的html代码:

<form id="form" action="javascript:void(0)">
<input type="submit" id="submit-reg" value="Register" class="submit button" onclick="showtemplate('anniversary')" style='font-family: georgia;font-size: 23px;font-weight: normal;color:white;margin-top:-3px;text-decoration: none;background-color: rgba(0, 0, 0, 0.53);'>    
</form>

this is my javascript code: 这是我的JavaScript代码:

function showtemplate(temp)
{
    $.ajax({
        type: "POST",
        url: 'ajax.php',
        data: "section="+temp ,
        success: function(data)
        {
            alert(data);
        }
    });  
}  

this is my ajax.php file: 这是我的ajax.php文件:

<?php
$ajax=$_POST['section'];
echo $ajax;
?>

The above html and javascript code is included in a file named slider.php. 上面的html和javascript代码包含在名为slide.php的文件中。 In my index file i have included this slider.php file and slider.php is inside slider folder. 在我的索引文件中,我已经包含了这个lider.php文件,并且slider.php位于slider文件夹内。 So basically index.php and slider.php are not inside the same folder. 因此,基本上index.php和slider.php不在同一文件夹中。

Javascript code alerts the data properly. Javascript代码会正确警告数据。 But in my php code (ajax.php file) the value of $_POST['section'] is empty. 但是在我的PHP代码(ajax.php文件)中,$ _ POST ['section']的值为空。 What is the problem with my code. 我的代码有什么问题。 I tried googling everything and tried a few codes but it still doesn't work. 我尝试使用Google搜索所有内容,并尝试了一些代码,但仍然无法正常工作。 Please help me out 请帮帮我

Try this instead: 尝试以下方法:

$.ajax({
        type: "POST",
        url: 'ajax.php',
        data: { 'section': temp},
        success: function(data)
        {
            alert(data);
        }
    }); 

It is quite possible that your server does not understand the string you have constructed ( "section="+temp ). 您的服务器很可能不理解您构造的字符串( "section="+temp )。 When using ajax I prefer sending objects since for an object to be valid it requires a certain format. 使用ajax我更喜欢发送objects因为ajax object有效,它需要某种格式。

EDIT1: 编辑1:

Try this and let me know if it doesn't work either: 试试这个,让我知道它是否也不起作用:

$.post('ajax.php', {'section': temp}, function(data}{
  alert(data);
});

Add jquery plugin(jQuery library) ,then only ajax call works for eg 添加jQuery插件(jQuery库),然后只有ajax调用适用于

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

check your input data , whether it contain '&,/' charators,then use encodeURIComponent() 检查您的输入数据,是否包含'&,/'字符,然后使用encodeURIComponent()

for Ajax Call Eg: 对于Ajax Call Eg:

var gym_name =  encodeURIComponent($("#gym_name").val());
$.ajax({
type: "POST",
url: "abc.php",
data:string_array,
success: function(msg) {
console.log(msg);
 }
});

In abc.php 在abc.php中

<?php
$id = $_POST['gym_name'];
echo "The id is ".id;
?>

Give a try to the following (although I cannot work out why @Grimbode's answer is not working): 尝试以下操作(尽管我无法弄清楚为什么@Grimbode的答案无效):

$("#submit-reg").on( "click", function() {
  $.ajax({
        type: "POST",
        url: 'ajax.php',
        data: {'section': 'anniversary'},
        success: function(data)
        {
            alert(data);
        }
    });
});

Note : I don't know what your underlying code is doing. 注意 :我不知道您的基础代码在做什么。 However, I would suggest not using HTML element properties to handle events for numerous reasons, but separate the JS/event handling appropriately (separate js file(s) (recommended) or inside <script> tags). 但是,出于多种原因,我建议不要使用HTML元素属性来处理事件,而应适当地分离JS /事件处理(建议使用单独的js文件(在<script>标记内)。 Read more... 阅读更多...

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

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