compare js var to php var

Discussion in 'PHP' started by roice, Nov 14, 2011.

  1. #1
    hello,

    I loaded data from DB and print it in form:

    query = mysql_query("SELECT cont FROM `some_table` WHERE id = $id ");
    $index = mysql_fetch_array($query);
    $cont  = $index['cont'];
    
    echo "<form method='post' action='index.php'>
    <textarea name='description' value='$cont'>$cont</textarea>
    </form>";
    PHP:
    I want the script to compare the content of value $cont with the new text that I'll write in the TEXTAREA
    I need it to be on the client side, means - when I click SUBMIT, the script will be run and will giving me massage if the text in the TEXTAREA was change (not equal to value $cont).

    Thank you all in advanced,
    Roi.
     
    roice, Nov 14, 2011 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    create a hidden variable where the value is also $cont
    then put a listener on the page which looks for change in the description box and puts up an alert if the hidden variable != to the textbox value
     
    sarahk, Nov 14, 2011 IP
  3. Jesse12

    Jesse12 Member

    Messages:
    360
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #3
    To compare the value in the textarea, use the below code:

    Add a method “comapreValues(dbValue)” on the onSubmit event of the form.

    And add the below code in javascript block.



    Function comapreValues(jsValue){

    currentValue=document.getElementByName(‘description’).value;
    if(currentValue != jsValue){
    alert(‘value mis-matched.’);
    document.getElementByName(‘description’).focus();
    return false;
    }
    Return true;
    }
     
    Jesse12, Nov 19, 2011 IP