Advanced Search

Results 1 to 6 of 6

Thread: dynamic change of an input (text)

  1. #1
    Join Date
    Apr 2008
    Posts
    58
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Default dynamic change of an input (text)

    Hello ,

    I'm having an issue when trying to change an input text value dynamically.
    here is the picture

    code :
    the page is written in php generating some javascript...

    purpose :
    dynamically control an input, and following this input changing the state of an other one (that will determinate if the form can be submited).

    info :
    - UserInputID input is the one to be checked in real time
    - validityID input is the one to be changed dynamically
    - isShortNameKey(event) keydown, controls the input key and do not allow some specific caracters (it works fine).
    - validateUserInput(event) , keyup event management, check the input result and check if already exists in database, then report it in targetDiv (it works fine).

    problem :
    After comparing the input with db, i would like to change the status of validityID input text.
    If input already exists indatabase, validityID should be set to NO, else to YES.

    php page
    PHP Code:
    [...]
    echo 
    '<input type="text" id="validityID" name="validityNAME" value="NO" />';

    echo 
    "<input type=\"text\" size=\"8\" maxlength=\"8\" id=\"UserInputID\" name=\"UserInputName\" value=\"\" style=\"background-color:#ccffbb\" on
    keypress=\"return isShortNameKey(event)\" onkeyup=\"validateUserInput(event)\" /></td>"
    ;
                            echo 
    "<td><span id=\"targetDiv\"><i>Machine Type validity</i></span>";
    [...]

    if (
    $dbresult == $userinput) {
                    echo 
    "<script type=\"text/javascript\">
                            setthisinputboxtextto('validityID','NO');
                    </script>"
    ;
                    echo 
    '<span style="color:red">not ok, it already exists</span>';
            } else {
                    echo 
    "<script type=\"text/javascript\">
                            setthisinputboxtextto('validityID','YES');
                    </script>"
    ;
                    echo 
    '<span style="color:green">ok it does not exists yet</span>';
            } 
    setthisinputboxtextto function (Javascript)
    Code:
    function setthisinputboxtextto(inputboxid, valuetoset) {
            if(!document.getElementById ) return; // Make sure browser supports getElementById
            var inputObj = document.getElementById(inputboxid);
    
            inputObj.value = valuetoset;
    
            alert('inputboxid : '+inputboxid);
            alert('valuetoset : '+valuetoset);
    }

    So ,
    - the (php) test comparing database and input is always working fine
    - but no alert comes up later in javascript ... and it never change the text content of validityID input box.


    If anyone can see something evident, that i missed
    Thanks for your help...

  2. #2
    Join Date
    Apr 2008
    Posts
    58
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Default

    Hi guys,

    Sorry to ask again, but i'm still stucked

    no one have a clue or work around ?

  3. #3
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Look into using Ajax.
    It allows you to run php via javascript then use any return values in the javascript.

    You have to remember php is server side and javascript is client side.

  4. #4
    Join Date
    Apr 2008
    Posts
    58
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your answer, but i'm a bit confused from what you wrote...
    Look into using Ajax.
    ??? isn't it what i'm doing ?
    It allows you to run php via javascript then use any return values in the javascript.
    I'm now willing to run javascript through php, not the invert.
    You have to remember php is server side and javascript is client side.
    Did i wrote anything that make you think i missanderstand this ???

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,627
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    there is no ajax in the code you posted. "ajax" is javascript using XHR. it sends a request back to your server, in the background, waits for a reply, and then processes it in some way.

    I'm sure the reason Joe reminded you that php works on the server is because the snippet you posted:
    PHP Code:
    echo "<input type=\"text\" size=\"8\" maxlength=\"8\" id=\"UserInputID\" name=\"UserInputName\" value=\"\" style=\"background-color:#ccffbb\" on
    keypress=\"return isShortNameKey(event)\" onkeyup=\"validateUserInput(event)\" /></td>"
    ;
                            echo 
    "<td><span id=\"targetDiv\"><i>Machine Type validity</i></span>";
    [...]

    if (
    $dbresult == $userinput) {
                    echo 
    "<script type=\"text/javascript\">
                            setthisinputboxtextto('validityID','NO');
                    </script>"
    ;
                    echo 
    '<span style="color:red">not ok, it already exists</span>';
            } 
    makes it look like you're printing some output, and then trying to wait for user input before printing more output to the page. this is not possible: once php has printed the page, it's done. you need to send a new request to do further processing.

    I understand that there may be other functions, that you didn't show above, that include your ajax functions and return values; but we can only offer advice based on what you show us.

    If you think you need to clarify anything for us, please do so.
    otherwise, I'd recommend learning more about AJAX. the jQuery library has very easy-to-use ajax functions.
    Last edited by traq; 05-28-2011 at 01:26 AM. Reason: clarification
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  6. #6
    Join Date
    Apr 2008
    Posts
    58
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Default

    sorry for the late reply.

    I'll try to figure out my problem with what you told me.
    I don't realy have time to learn about Jquery anyway ... i've been asked to avoid third party softwares/libraries.

    thanks both reply.

Bookmarks

Posting Permissions

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