SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I pass a javascript variable into php without redirection?

    How do I pass a javascript variable into php without redirection?
    For example the variable publisherid, how do I use it inside some php code


    Code JavaScript:
    <script type="text/javascript">
    publisherid = 'something';
    variable1 = 'something';
    variable2 = 'something';
    //your code can access these variables!
    </script>
    <script type="text/javascript" src="http://example.com/your-ad-serving-script.php"></script>

  2. #2
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP is a server side language which means that it is 100&#37; finished before JavaScript gets a chance to access it. What are you trying to accomplish?
    MySQL v5.1.58
    PHP v5.3.6

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One way could be to put js variable in form hidden field value and then after php_self submit you will have it on that same page as variable $_POST(or GET)['hiddenFieldName']

  4. #4
    SitePoint Addict richtestani's Avatar
    Join Date
    Nov 2003
    Location
    Bridgeport
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could have PHP write out tha javascript

    either like:
    [code]
    <script>
    var phpVar="<?php echo $jsVar; ?>";
    </script>

    or have php write all the javascript.
    RichTestani
    -------------------------------
    http://www.junkdepot.com
    http://www.rareoopdvds.com | The Movie Poster Site

  5. #5
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    To pass from javascript TO PHP, you can use AJAX.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wonshikee View Post
    To pass from javascript TO PHP, you can use AJAX.
    please could i have any examples
    i'm need your help to create an ad company
    And I cannot go any further by not knowing how to make the javascript variable be passed to the php page.

    This is important because the javascript variable corresponds to the id of the ad code which is different for each user.

    Therefore, the php page retreives all the data from the javascript variable.

    A classic example is a javascript statcounter script which gets the screen resolution and then the php page inserts it into a mysql database.
    Last edited by desbest; May 24, 2007 at 02:26.

  7. #7
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <script type='text/javascript'>
    function ajaxFunc() {
                    var data1; // populate however you want
                    var data2; // same here
    		var ajaxReq; 
    		try {
    			// Opera 8.0+, Firefox, Safari
    		ajaxReq = new XMLHttpRequest();
    		} catch (e){
    			// Internet Explorer Browsers
    			try {
    				ajaxReq = new ActiveXObject("Msxml2.XMLHTTP");
    			} catch (e) {
    				try {
    					ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e) {
    					// Something went wrong
    					alert("Your browser broke!");
    					return false;
    				}
    			}
    		}
    		var queryString = "?id="+data1+"&id2="+data2;
    
    		ajaxReq.open("GET", 'ajaxscript.php' + queryString, true);
    		ajaxReq.onreadystatechange = function() {
    			if(ajaxReq.readyState == 4){
    				response  = ajaxReq.responseText;
                                    alert(response);
    			}
    		}
    		ajaxReq.send(null); 
    }
    </script>

    Invoke the ajaxFunc() however fits your needs. Populate the necessary data you will be passing. Then put the code below in your ajaxscript.php.

    That's all you should need to understand how to get you started in Ajax.

    PHP Code:
    <?php
        
    echo "Your first data is : ".$_GET['id']." and second is : ".$_GET['id2'];
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •