compare js var to php var

Discussion in 'JavaScript' 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. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #2
    
    echo "<script type='text/javascript'>";
    echo "function chkSubmit() {";
    echo "    var cont = '$cont'";
    echo "    if (document.getElementById( 'description' ).value == cont) {";
    echo "          alert('Value matches');";
    echo "        //document.getElementById( 'formname' ).submit();";
    echo "    } else {";
    echo "          alert('Value does not match');";
    echo "        //document.getElementById( 'formid' ).submit();";
    echo "    }";
    echo "}";
    echo "</script>";
    echo "<form method='post' name='formname' id='formid' action='index.php'><textarea name='description' id='description'>$cont</textarea>";
    echo "<input type='button' name='process' value='Submit' onclick='javascript:chkSubmit();'/>";
    echo "</form>";
    
    Code (markup):
    Submit of the form is disabled, so if you want to submit the form in the case of match or a missmatch then just uncomment the submit line in javascript //document.getElementById( 'formid' ).submit(); by removing //
     
    pr0t0n, Nov 14, 2011 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you!
     
    roice, Nov 15, 2011 IP
  4. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #4
    
    echo "        //document.getElementById( 'formname' ).submit();";
    
    Code (markup):
    should be
    
    echo "        //document.getElementById( 'formid' ).submit();";
    
    Code (markup):
    in line 6. Oops :)
     
    pr0t0n, Nov 15, 2011 IP