0

I'v been trying for last 2 hours to pass parameters from my jquery to PHP. I cannot seem to figure this out. So my code goes following

var something = getUrlParameter('month');
function Refresh()
{ 
$.ajax({

        type: 'POST',
        url: 'getCalendar.php',
        data: {test:something},
        success: function(data){
                 if(data != null) $("#calendarDiv").html(data)
         }
     });
}

getUrlParameter is

function getUrlParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}​

I just cant seem to be able to pass anything to my php file. Thanks. My goal is to pass ?month=something&year=something into PHP file, so I can based on that display calendar.

Url of example: http://chanceity.com/calendartest.html

But it doesn't work because my php file is not getting those params.

4
  • How do you call function Refresh()? Have you checked your browser console for any js errors, or if your ajax() is firing? Commented Aug 28, 2014 at 20:08
  • My ajax is going trough, it is displaying calendar I want. Commented Aug 28, 2014 at 20:09
  • Why not add it to your .ajax() url -> url: 'getCalendar.php?month='+getUrlParameter('month')+'year='+getUrlParameter('year'), Commented Aug 28, 2014 at 20:18
  • Don't really have much knowledge with ajax so I'm trying this way, turns out it was function error, now I have tried with function $.urlParam = function(name){ var results = new RegExp('[\?&amp;]' + name + '=([^&amp;#]*)').exec(window.location.href); return results[1] || 0; } but if I try to send parameter, for example September gets cut into Se for some reason Commented Aug 28, 2014 at 20:23

2 Answers 2

0
$.ajax({
    type: 'POST',
    url: 'getCalendar.php',
    cache: false,
    dataType:'json',
    data: "data=" + {test:something},
    success: function(data)
     {
         $("#calendarDiv").html(data)
     },
    error:function()
     {
        $("#calendarDiv").html('Could not get results')
     }
 });

and next go to your php file get the results and echo the variable back thats it

$value = htmlentities($_GET['data']);

if(!empty($value))
{
   $results = 'action you want to do ';
}
else
{
   $results = '';
}
echo json_encode($results);
Sign up to request clarification or add additional context in comments.

Comments

0

You can also do it with just javascript

function myJavascriptFunction() { 
  var javascriptVariable = "John";
  window.location.href = "myphpfile.php?name=" + javascriptVariable; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.