Results 1 to 3 of 3

Thread: Ajax in JavaScript to pass to PHP

  1. #1
    Join Date
    Dec 2012

    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:

      var radioPressed = 123;
      <?php $what_is_myVar = radioPressed ; ?>
    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 10:31 AM.

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

    <!doctype html>
    		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    		<meta name="language" content="en" />  
    		<script type="text/javascript">
    				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;
    	<body onload="passVariable()">
    		<?php echo $_REQUEST['x']; ?>
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Dec 2012
    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):

    <script type="text/javascript">
      $('#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 } ?>                
    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