1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Two functions within javascript onchange

Discussion in 'PHP' started by Rubin Remus, Oct 28, 2011.

  1. #1
    Hi everyone,

    I have looked around the web for the solution to this - and I have found 'solutions' in a sense, but I can't get them to work how I need them to. Basically, I think because the page reloads onchange the other function won't stick.

    If someone can confirm my concerns or provide a solution, I'd be grateful.

    Here's the relevant code:

    This is where I want the functions to be called, and as you can see, one already is - and that's working fine...

    <select name='address' id="address" onchange="document.location.href = 'stepthree.php?defaddress=' + this.value;">
    Code (markup):
    However, I want to implement this into it:

    <script type="text/javascript">
    function addressentry() { 
    if (document.stdaddress.address.value == '1') {
    document.form.line1.disabled=true;
    // return false; // not sure this line is needed
    } else {
    document.form.line1.disabled=false;
    // return false; // not sure this line is needed
    }
    } 
    </script>
    Code (markup):
    I have tried this, with no success:

    <select name='address' id="address" onchange="document.location.href = 'stepthree.php?defaddress=' + this.value; addressentry();">
    Code (markup):
    Although it does look like the field flickers to 'disabled' and then becomes active again.

    I figure this is down to the page reloading, but if anyone can provide a solution, it'd be great!
     
    Rubin Remus, Oct 28, 2011 IP
  2. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #2
    So do you want to set document.form.line1.disabled=true; and then reload the page? Or call addressentry() after the page has reloaded?
     
    mfscripts, Oct 28, 2011 IP
  3. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #3
    By the way you posted this in a PHP forum, it should be Javascript.
     
    mfscripts, Oct 28, 2011 IP
  4. Rubin Remus

    Rubin Remus Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for your reply.

    My fault (sorry admins!) - I kind of assumed it could go in either, since the file is a php file.

    Logically, I guess I want the addressentry() to be called on reload. That way it will disable fields which have been filled from the dropdown menu.

    How would I do this?
     
    Rubin Remus, Oct 28, 2011 IP
  5. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #5
    addressentry won't be called after the page reloads.

    There are ways to do it, in your HTML, by adding the function to run at the bottom of all your content (on step three).
    <body>
    <!--[All your HTML code goes here]-->
    <script type="text/javascript">
             addressentry();
    </script>
    </body>
    
    HTML:
    If you are trying to pass data on page to page, you are going to need to use cookies in PHP or Javascript. Or get/post.
     
    blueparukia, Oct 28, 2011 IP
  6. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #6
    I wouldn't call it via Javascript on reload. I'd look for the address param within PHP and set the disabled attribute as required.

    So on a reload on the element you want to disable add this to where the DISABLED attribute would normally go.

    
    if((isset($_GET['address'])) && ((int)$_GET['address'] == 1))
    {
        echo 'DISABLED';
    }
    
    PHP:
     
    Last edited: Oct 28, 2011
    mfscripts, Oct 28, 2011 IP
  7. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #7
    That would be a better way since it would gracefully degrade for users without scripts enabled.

    Though the proper HTML to output would be:

    
    $disabled = 'disabled="disabled";
    ...
    echo '<input '.$disabled.' name="addressline1" />'; 
     
    PHP:
     
    blueparukia, Oct 28, 2011 IP