burakim
burakim

Reputation: 482

Passing Get value to different jquery file

I'm newbie at Jquery. I stacked to passing php $_GET['myvalue'] to my jquery file.

I struggled very much but I cannot find solution.

I have different one php file and one jquery file. I call my php file like

myphpfile.php?myvalue=testvalue

Then I want to receive myvalue's value to my jquery file. I tried document.getElementById but It doesnt work.

For any helping Thanks.

Upvotes: 0

Views: 69

Answers (4)

JohnnyFaldo
JohnnyFaldo

Reputation: 4161

If you want to access the $_GET['myvalue'] value in your Javascript you can echo it out straight into it.

<script type="text/javascript"> 
   // Your code 
   // The var you want to assign it to 
   var value = '<?php echo $_GET['myvalue'];?>'
</script>

Upvotes: 2

Maciej A. Czyzewski
Maciej A. Czyzewski

Reputation: 1529

If you want to access the $_GET['myvalue'] value in your script but using only Javascript.

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

Upvotes: 1

mcriecken
mcriecken

Reputation: 3297

Try this in your jQuery File:

First create a function to parse the parameters in the URL:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
        vars[key] = value;
    });
    return vars;
}

Then you call the function:

var myUrlParameters = getUrlVars();

Then you can access them with:

var myParameter = myUrlParameters['myParameter'];

Upvotes: 1

Alvaro
Alvaro

Reputation: 41595

You need to read more about jQuery and Javascript...

You might want to add the variable you want in an HTML element, for example:

PHP

<?php
    $myValue = $_GET["myvalue"];
    echo '<input type="hidden" name="_method" value="'.$myValue.'" id="myElement" />';
?>

At jQuery:

$(document).ready(function(){
    alert($('#myElement'));
});

Upvotes: 1

Related Questions