Passing URL parameters using jquery in php

I have been trying for the last 2 hours to pass parameters from my jquery to PHP. I can not understand this. Therefore my code goes for

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

 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 can't transfer anything to the php file. Thank you My goal is to convey? Month = something & year = something in a PHP file, so I can rely on this display calendar.

Example URL: http://chanceity.com/calendartest.html

But this does not work, because my php file does not receive these parameters.

0
source share
2 answers
 $.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 then go to your php file to get the results and echo back to back again.

 $value = htmlentities($_GET['data']); if(!empty($value)) { $results = 'action you want to do '; } else { $results = ''; } echo json_encode($results); 
0
source

You can also do this only with javascript

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

Source: https://habr.com/ru/post/974081/


All Articles