I'm trying to compare the value of 2 textareas, when you click the compare button the output will be displayed in the 3 text area. So what I want to happen is there are two inputs when the button is click thePHP
file compares them and returns the result to the 3rd text area. How can I make this work? here's my code below and here's the jsfiddle : http://jsfiddle.net/jVxhp/
index page
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#Compare').click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'Corrections.php',
data: {var1:address, var2:res},
success: function(data)
{
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
document.getElementById('address').value;
document.getElementById('res').value;
document.getElementById('cor').value='Corrections.php';
}
});
});
});
</script>
<textarea type="text" id="address"
onfocus="if(this.value===this.defaultValue)this.value=''" onblur="if(this.value==='')this.value=this.defaultValue"
>Input1</textarea>
<textarea type="text" id="res" onfocus="if(this.value===this.defaultValue)this.value=''" onblur="if(this.value==='')this.value=this.defaultValue"
>Input2</textarea>
<textarea type="text" id="cor" style="color: silver" disabled>
Output</textarea>
<div id="btn1"><input type="button" value="Compare" onClick="valbtn()"></input></div>
Corrections.php
<?php
$str1 = address;//address is textarea1 that holds one of the values to be compared
$str2 = res;//res is textarea2 were textarea1 will be compared
$tempArr;
$var2;
$ctr=0;
echo "Input:<br> $str1 <br><br>Output:<br> $str2";
$strarr = (explode(" ",$str1));
echo("<br>");
$strarr2 = (explode(" ",$str2));
echo("<br>");
if(sizeof($strarr) > sizeof($strarr2)){
$result = array_diff($strarr,$strarr2);
}else{
$result = array_diff($strarr2,$strarr);
}
if (count($result) > 0){
echo "Added:<br> | " ;
foreach ($result AS $result){
echo $result." | ";
}
}
echo "<br>";
for($i=0;$i<count($strarr);$i++) {
if (strcasecmp($strarr[$i], $strarr2[$i]) != 0)
{
$var[$ctr] = strcasecmp($strarr[$i], $strarr2[$i]);
$var2[$ctr] = $i;
$ctr +=1;
}
}
$tempArr = preg_grep('/[0-9]/',$var);
echo"<br>Changes: <br>";
for($i=0;$i<count($var);$i++) {
echo $strarr[$var2[$i]] . ' is changed to ' . $strarr2[$var2[$i]] . '<br>';
}