1

I have two files one php and one html, the html file serves as the users interface where the users input their queries, the php files serves as the process or where the event will happen then it will return them to the html file for the output. According to my friend the best way to link the two is using jquery or ajax which I'm not quite sure. I tried to link them using this code but it didn't work if you can help me find my mistake I would gladly appreciate it.

HTML File

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
    $('#setVal').on('click', function () {
        var form = $('.buildaddress').not('#formatted_address');
        var vals = form.map(function () {
            var value = $.trim(this.value);
            return value ? value : undefined;
        }).get();
        $('#formatted_address').val(vals.join(', '));
</script>

when I added this part the link didn't work

<script>
            $('#Compare').click(function(e) {
                e.preventDefault();
                var address = $('#address').val();
                var formatted_address = $('#formatted_address').val();
                console.log(address);
                console.log(formatted_address);
                $.ajax({
                    type: 'POST',
                    url: 'Corrections.php',
                    data: {
                        var1: address,
                        var2: formatted_address
                    },
                    success: function(data) {
                        document.getElementById('cor').value = data;
                    }
                });
            });
        });
    </script>

PHP file

<?php
$str1 = $_POST['var1']; 
$str2 = $_POST['var2'];
$tempArr;
$var2;
$ctr=0;

echo "Input: $str1\n";
echo  "Output: $str2\n";
?>
2
  • 1
    try using a console in your browser. Personally I like firebug for firefox. It should show you the ajax request going out and what kind of response it returns if any. Commented Jan 23, 2014 at 9:54
  • Indenting your code correctly (which you almost did) should help you find this kind of issues. And use a console as well, as suggested! Commented Jan 23, 2014 at 9:59

1 Answer 1

3

You have an extra }); in your script. Just remove the extra }); in your second script, your code will work

<script>
        $('#Compare').click(function(e) {
            e.preventDefault();
            var address = $('#address').val();
            var formatted_address = $('#formatted_address').val();
            console.log(address);
            console.log(formatted_address);
            $.ajax({
                type: 'POST',
                url: 'Corrections.php',
                data: {
                    var1: address,
                    var2: formatted_address
                },
                success: function(data) {
                    document.getElementById('cor').value = data;
                }
            });
        });
    //}); should be removed
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

it was also an error, but it still din't work tnx for pointing it out
@user3046019 may be the ajax call return an error, try adding error function and check. Note that If the ajax calls php, then it will be considered as success. You have an error in your php code
Simply try alert(data) in success and check what the response contains

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.