0

this is my html code:

<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:

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

this is my ajax.php file:

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

The above html and javascript code is included in a file named slider.php. In my index file i have included this slider.php file and slider.php is inside slider folder. So basically index.php and slider.php are not inside the same folder.

Javascript code alerts the data properly. But in my php code (ajax.php file) the value of $_POST['section'] is empty. What is the problem with my code. I tried googling everything and tried a few codes but it still doesn't work. Please help me out

Ben
  • 8,894
  • 7
  • 44
  • 80
phpidz
  • 39
  • 2
  • 9

3 Answers3

2

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 ). When using ajax I prefer sending objects since for an object to be valid it requires a certain format.

EDIT1:

Try this and let me know if it doesn't work either:

$.post('ajax.php', {'section': temp}, function(data}{
  alert(data);
});
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
  • I tried doing that before. But it is not working. it is not retrieving anything. The $ajax variable of ajax.php file is empty. – phpidz Aug 11 '15 at 08:51
  • @phpidz empty or not set? In fact check what value temp has. Add an alert when you enter your function to see if it is set. – kemicofa ghost Aug 11 '15 at 09:01
  • Yes the value of temp is not empty. It is alerting correct data @grimbode – phpidz Aug 11 '15 at 09:17
0

Add jquery plugin(jQuery library) ,then only ajax call works for eg

<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()

for 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

<?php
$id = $_POST['gym_name'];
echo "The id is ".id;
?>
Nitheesh K P
  • 1,090
  • 8
  • 17
  • No my input doesn't contain such characters. I tried the code. No output. I am trying this code since a month but it is not working only. :( – phpidz Aug 11 '15 at 09:41
  • sorry.. i missed one line of code after assigning variable gym_name ,pls add var string_array="& gym_name ="+ gym_name; Bcoz here passing a string_array variable. – Nitheesh K P Aug 12 '15 at 06:04
0

Give a try to the following (although I cannot work out why @Grimbode's answer is not working):

$("#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). Read more...

Community
  • 1
  • 1
Manu
  • 185
  • 4
  • 11
  • I think I got why your initial code does not work. Your onclick is rendered there is no definition for your showtemplate function as you probably load it after the input element is rendered at the end of your HTML page by either using script tags or loading the script. You could instead put that script above your form and try again. – Manu Aug 11 '15 at 11:25
  • Thank you for your reply everyone. The problem is not in my javascript code. It lies somewhere in my php code. My ajax is working and it is alerting the correct data. But im my ajax.php i am trying to output that data but it shows nothing. I want to pass that ajax data into my php code. Is that possible? – phpidz Aug 12 '15 at 05:35
  • Do you have a live example page? – Manu Aug 12 '15 at 05:42
  • http://www.wishafriend.com/greetings/ This is my page. Click on the register button on the slider. It alerts the data. But i want to pass it in php. – phpidz Aug 12 '15 at 06:23
  • Indeed there is no problem with your JS. I clicked on the register button and it popped up an alert box with **anniversary**. I also sent a custom post to your script ajax.php and I got the posted data in response. I don't get what is your problem now..I mean the script returns the correct data, the alert box gets the data from your script upon success of the ajax POST, which is the variable `$ajax`, and in turn `$_POST['section']`, which means `$_POST['section']` is not empty. – Manu Aug 12 '15 at 06:41
  • I want to use $_POST['section'] further in my code. Now when the data is returned to the ajax code, I want to use $_POST['section'] in my php code but when i try to retrieve $_POST['section'] it shows that it is empty. – phpidz Aug 12 '15 at 07:00
  • `$ajax` variable is not empty, that means `$_POST['section']` in your script. That means that you can use `$ajax` variable further in your code. Once you echo something and exit further execution then the (last) echo will be sent back to the ajax call. Pardon me if I don't see the problem, I'm trying to help. – Manu Aug 12 '15 at 07:04