#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    12

    Unhappy How can I get variable from JavaScript to PHP without refresh the page


    Hi, All

    PHP Code:

    print("Attributes:");
    print("<SELECT NAME='attributes' onChange='changeAttribute(this.form.attributes)'>");
    print("<OPTION VALUE='' SELECTED>Choose a web page");
    print("<OPTION VALUE='title'>Title");
    print("<OPTION VALUE='link'>Link");
    print("<OPTION VALUE='image'>Image");
    print("<OPTION VALUE='para'>Paragraph");
    print("<OPTION VALUE='footer'>Footer");
    print("</SELECT><BR>");
    print("<br>");

    JavaScript Code:
    function changeAttribute(theAttribute)
    {
    newAttribute = theAttribute.options[theAttribute.selectedIndex].value

    if (newAttribute !="")
    {
    attribute=newAttribute
    }
    }

    the default value of "attribute" is "title", what I want to do is, once I change the option in select, let says, change to "Link", the JavaScript assigns the variable "attribute" is "Link", but how can I get the variable "attribute" in JavaScript and pass to PHP.
    (They are put into same file, the Javascript is embedded in the PHP file)
    Can I do that without refreshing the page?
    If no, need to refresh, once refresh, how can I remain the value of the select after I refresh the page. Because normally, the page is reloaded, all variable is initialized. How can pass it from JavaScript to PHP and keep the status of the select after I refresh it?

    Thanks!
    Jacky =)
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    645
    Rep Power
    0
    you will need to get all the stuff you want from php and put it in javascript arrays. that way you can get javascript to change the value on the fly without refreshing.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    12

    But...I want to get the form attribute into another form attribute


    Hi,

    I know what you mean, but...

    Other than the code as shown above,

    PHP Code:

    print("Background Color");
    print("<SELECT NAME='backgroundColor' onChange='changeBackgroundColor(this.form.backgroundColor)'>");
    print("<OPTION VALUE='' SELECTED>Choose background color");
    print("<OPTION VALUE='blue'>BLUE");
    print("<OPTION VALUE='green'>GREEN");
    print("<OPTION VALUE='red'>RED");
    print("<OPTION VALUE='orange'>ORANGE");
    print("<OPTION VALUE='white'>WHITE");
    print("<OPTION VALUE='gray'>GREY");
    print("</SELECT>");
    print("<BR>");
    print("<INPUT TYPE=HIDDEN NAME='bg_color_attribute' VALUE=??>");

    I want to get the value from the "Select" name="Attributes", then apply to this code, as the value of this "select" name="backgroundColor".

    In my way, as I change the option in the Attribute select, then the variable in Javascript called "attribute" will store the attribute value, says "Link". Then I want to get this value from JavaScript to PHP as the value of this "Select" named "backgroundColor".
    Is it possible?

    Or

    Can I get the value of selected option in "Attribute" select then apply as the value of another select called "backgroundColor"?
    Help, IN URGENT!
    Thanks!

    Regards,
    Jacky =)
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,935
    Rep Power
    4033

    Possible, but may not be pratical


    If all you want to do is send the value to php but not get anything back, it can be done rather eaisly using images. If you want to get something back from the script, that's possible to, but may just be easier to reload the page. I saw something on the net once about using cookies to move things around, never tried it my self, but it looked like a good idea.

    If you just want to send data, you can do something like this:

    add a 1x1 image to your page that you will use to move the data.
    <IMG SRC="setValue.php" WIDTH="1" HEIGHT="1" NAME="dataImage">


    Then in your javascript, just do this when the value changes:
    document.dataImage.src="setValue.php?value="+escape(attribute);

    Then you just make setValue.php do whatever needs to be done w/ $_GET['data'] and have it return a trasparent gif image. Something like this maybe:

    PHP Code:
    <?php
    $fp
    =fopen('info.txt','w');
    fwrite($fp$_GET['data']);
    fclose($fp);
    header("Content-type: image/gif");
    readfile("blank.gif");
    ?>
    The cookies one is a bit more involved, so I'm not gonna post it here, but if it interests you try looking on google for it or ask back and I could try to put something together (after I find out if it even works first).
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    645
    Rep Power
    0
    and my last post still stands.

    tell me if I am wrong.

    you have 2 select boxes and in one select box you have

    <select name="attributes">
    body, link, text

    </select>

    and in another you want to list the things associated to what ever is selected in the first select box, correct.

    so if I picked "link" then the second box would display the colors to choos from. if I picked body then that will load the colors I have for that selection, am I correct?
  10. #6
  11. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,935
    Rep Power
    4033
    Sounds to me like you just want to change that hidden input to whatever they selected. If it's on the page w/ the attributes select box, then just use javascript and do something like this for your function:

    function changeAttrib(frm){
    newAttrib=frm.attribute[frm.attribute.selectedIndex].value;
    if (newAttrib != ''){
    frm.bg_color_attribute.value=newAttrib;
    }

    }


    If they arn't on the same page, you are going to be needing to reload the page anyway, so just do it like one normaly would w/ a form submission.

IMN logo majestic logo threadwatch logo seochat tools logo