0

I want to pass information through a Jquery scripts onto a php file. I don't want it to be handled by a form, I just want it to grab it from the URL and send it to the php file.

Lets say I typ in this URL:

index.html?info=1

I want that to be passed by the script so it can be prossed.

HTML:

<script type="text/javascript">

$(document).ready(function(){
   var j = jQuery.noConflict();
    j(document).ready(function()
    {
        j(".refresh").everyTime(1000,function(i){
            j.ajax({
              url: "info.php?info=<?php echo $_GET['info']; ?>",
              cache: false,
              success: function(html){
                j(".refresh").html(html);
              }
            })
        })
    });
});



</script>
</head>
<body>
<div class="refresh">This will get Refreshed in 10 Seconds</div>

PHP:

<?php
$info = $_GET['info'];
if($info=='1')
{
?>
passing works.
<?php
}
?>

So as you can see I tried this:

url: "refresh.php?info=<?php echo $_GET['info']; ?>",

But that doesn't work. Just to wrap it up. I want to typ in an URL and retrieve the GET data from it. I dont want to retrieve it by having a form sendin an GET method.

I tried multiple things but they all turned out to be form based But like I said, Im redirecting the user to the page with set information depending on the users state. So the info must be taken purly out of the URL

Code:

Type: 'GET',
URL: 'info.php',
Data: 'info.php' + $('#input-field-which-i-dont-want').vall()

If you have any questions, please feel free to ask away.

Thanks in advance.

  • 1
    your homepage is index.html (HTML). Is your apache configured to process php on html pages? If not, consider using index.php since you are trying to use php methods to access information in the request. – skrilled Jul 25 '14 at 17:44
  • Lel, forgot to put index.html into php mode.... thanks for mentioning it xD – user3813511 Jul 25 '14 at 17:47
  • By making your HTML page be parsed by PHP, it won't be cached – Ruan Mendes Jul 25 '14 at 17:50
  • So, you get the parameter in PHP, write it to JS, and then you pass it again to PHP. Why? – Oriol Jul 25 '14 at 17:51

1 Answers1

0

You don't need jQuery to get parameters from a URL

function getUrlParams() {

  var paramMap = {};
  if (location.search.length == 0) {
    return paramMap;
  }
  var parts = location.search.substring(1).split("&");

  for (var i = 0; i < parts.length; i ++) {
    var component = parts[i].split("=");
    paramMap [decodeURIComponent(component[0])] = decodeURIComponent(component[1]);
  }
  return paramMap;
}

And your script can use it like

$(document).ready(function(){
   var j = jQuery.noConflict();
    var params = getUrlParams();
    j(document).ready(function()
    {
        j(".refresh").everyTime(1000,function(i){
            j.ajax({
              url: "info.php?info=" + encodeURIComponent( params.info ),
              cache: false,
              success: function(html){
                j(".refresh").html(html);
              }
            })
        })
    });
});
Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • @user3813511 A comment like "doesn't work at all" doesn't help at all. What is the error message? What did happen? I've edited the answer, try it again. And if it doesn't work, be sure to provide meaningful feedback. – Ruan Mendes Jul 25 '14 at 19:46