1

So, heres a super simple jQuery / AJAX comment system step one. Step two is the PHP to insert the data into the DB. I need the $_GET['variable'] for the page / a $_SERVER['variable'] to store into the DB. How can I get these in the jquery. I can't just say $spot = $_GET['spot'] or $url = $_SERVER['FILE_SCRIPTNAME'] in the PHP. It won't pick it up. I has to be sent through the jQuery / AJAX. How can I do this?

$(document).ready(function() {
        $('#submit_comment').click(function() {
            var comment = $('#place_comment').val();
            // Somewhere here set the $_GET['variable'];
            $.ajax({
                type: 'POST',
                url: 'http://localhost/app/comment/comment.func.php',
                data: 'comment='+comment,
                success: function(data) {
                    $('#replies').html(data);
                    }
                });
            });
        });
1
  • You might know this already, but storing $_GET variables, or an user input, directly in the database is a very bad security practice. NEVER TRUST user input. You need to escape it before you store it, and escape it when you retrieve it, or you will have some nasty XXS security holes at least. SQL injection and nastier things are also a danger. Commented Nov 19, 2011 at 4:45

2 Answers 2

2

If I understand what you are trying to do correctly, you can try something like this to use server-side PHP variables in your client-side javascript.

var comment = $('#place_comment').val();
var myVariable = '<?php echo $_GET['variable'] ?>';
Sign up to request clarification or add additional context in comments.

1 Comment

@Byran is there any possibility in jquery? You used php.
-1

You can't do this like this : javascript is client-side and PHP $SERVER array is server-side.

Comments

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.