www.webdeveloper.com
Results 1 to 3 of 3

Thread: Ajax in JavaScript to pass to PHP

  1. #1
    Join Date
    Dec 2012
    Posts
    5

    Ajax in JavaScript to pass to PHP

    I’m using radio buttons (‘countries’) to open dropdown-box of its corresponding ‘cities’.
    The variable ‘radioPressed’ detects the selected radio button and I need to pass its value inside my JavaScript to a PHP, as follows:

    Code:
    <script>
      var radioPressed = 123;
      <?php $what_is_myVar = radioPressed ; ?>
    </script>
    Since the solution can be complicated, I would ask to receive an advice only regarding the main steps of how to pass the value of 'radioPressed' into $what_is_myVar

    Thanks a lot for any assistance,
    Last edited by blsn; 01-07-2013 at 09:31 AM.

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,112
    Here's an example. I've used "x" instead of radioPressed for the variable.

    Code:
    <!doctype html>
    
    <html>
    	<head>
    		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    		<title>Title</title>
    		<meta name="language" content="en" />  
    		<script type="text/javascript">
    			//<![CDATA[  
    				function passVariable(){
    				
    					// set your variable
    					var x = '123';
    					
    					// get the current url and append your variable
    					var url = document.location.href + '?x=' + x;
    					
    					// to prevent looping, check to make sure your current url contains a common string
    					var exists = document.location.href.indexOf('?x=');
    					if(exists < 0){
    						// redirect passing your variable in
    						window.location = url;
    					}
    					
    				}
    			//]]> 
    		</script>
    	</head>
    	<body onload="passVariable()">
    		<?php echo $_REQUEST['x']; ?>
    	</body>
    </html>
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Dec 2012
    Posts
    5
    Thanks very much for your professional reply, works perfect but I need to have the PHP variable inside JavaScript, because the PHP code for opening the dropdown-box of matching cities is inside the JavaScript (see below my working script):

    Code:
    <script type="text/javascript">
    $(document).ready(function(){
      $('#locationTree input:radio').change(function() {
        var radioPressed = $('input:radio[name=Region]:checked').val();
    
        if(radioPressed == 123) {
          var listCities = $("select[name='City']");
          <?php View::newInstance()->_exportVariableToView(123); ?>
          		<?php while(has_list_cities()) { ?>
            	$("<option><?php echo list_city_name(); ?></option>").appendTo(listCities);
          <?php } ?>                
        } 
    
      });   
    });
    </script>
    In the code above I need to use $_REQUEST['x'] as follows:
    <?php View::newInstance()->_exportVariableToView($_REQUEST['x']); ?>

    Is there any way to get the PHP variable inside JavaScript?

    Your help is greatly appreciated,

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles