Have javascript right, but need to pass variable with ajax, GIVING REP!

Discussion in 'JavaScript' started by MarcL, Feb 9, 2011.

  1. #1
    Have javascript right, but need to pass variable with ajax, WILL GIVE REP!

    
    function showBox() {
    var selectBox = document.getElementById("store").value;
    	if (selectBox == 'N') {
    	document.getElementById("penyard").style.display="none";
    	}else{
    	document.getElementById("penyard").style.display="block";
    		}
    }
    
    Code (markup):
    so how do I pull the value of selectBox to php without refreshing the page.
    penyard needs the value of selectBox to know what to display
     
    MarcL, Feb 9, 2011 IP
  2. fri3ndly

    fri3ndly Active Member

    Messages:
    111
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Have a look at AJAX with jQuery (http://api.jquery.com/jQuery.ajax/)

    Load up the jquery library

    Then some code like this will sort you out:

    
    
    function showBox()
    {
        // Set selectBox variable based on #store value
        $selectBox = $("#store").val();
        
        // Show & Hide
        if ($selectBox == 'N')
        {
    	$("#penyard").css('display', 'none');
        }
        else{
    	$("#penyard").css('display', 'block');
        }
    
        // Send data to PHP script via AJAX
        $.ajax({
            url: "path-to-your-php-script.php",
            type: "POST",
            data: {selectbox: $selectBox},
            success: function()
            {
                // Anything you want to happen on success here
            }
        });
    }
    
    
    Code (markup):
    In your php script you just need to get the content of $_POST['selectbox'];

    Hope this helps (have not tested syntax)
     
    fri3ndly, Feb 17, 2011 IP
  3. MarcL

    MarcL Notable Member

    Messages:
    4,265
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    215
    #3
    I figured it out for the create a new feature using a method similar to this (yes used the POST)...

    But to edit a item using this its hard because onLoad doesnt work in <select> / <option> environments... Where when creating new its just OnChange if I remember right.
     
    MarcL, Feb 17, 2011 IP
  4. fri3ndly

    fri3ndly Active Member

    Messages:
    111
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Please can you clarify what you mean? Do you mean you can't conrol whatever #penyard is? If not it is probably because it need to be added to the DOM (http://api.jquery.com/bind/)
     
    fri3ndly, Feb 17, 2011 IP
  5. MarcL

    MarcL Notable Member

    Messages:
    4,265
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    215
    #5
    No I am trying to pre set it to what people have it at for their account.
     
    MarcL, Feb 17, 2011 IP
  6. oenda

    oenda Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I will advise learning jQuery if you would like to work with AJAX as the two go hand in hand and are seemingly easier than pure javascript using XMLHttpRequest to use AJAX.
     
    oenda, Feb 17, 2011 IP