1


I'm trying to pass a php variable from page1.html (which contains a php variable that I got from another form) to page2.php using ajax but it's not working, so I need your help.

code from page1.php:

<script>
var url = 'page2.php';
$("#save").click( function() {
    $.ajax({
        url : url,
        type: 'post',
        data : {
            admin : <?php echo $admin; ?>,
            gname : <?php echo $gname; ?>,
            filename : docname,
            content : encodeURIComponent(editor.getValue())
        }
    });
});
</script>

The datas filename and content are being sent successfully but admin and gname are not.

Thanks in advance.

UPDATE:

<?php
    if(isset($_GET['admin'])) {
    $admin = $_GET['admin'];
    }
    if(isset($_GET['gname'])) {
    $gname = $_GET['gname'];
    }
?>

This code is where I get the php variable that I want to send, from another form which is irrelevant to this question.

6 Answers 6

3

Here you go buddy, replace your script code with this.

<script>
var url = 'page2.php';
var admin = '<?php echo $admin; ?>';
var gname = '<?php echo $gname; ?>';
$("#save").click( function() {
    $.ajax({
        url : url,
        type: 'post',
        data : {
            admin : admin,
            gname : gname,
            filename : docname,
            content : encodeURIComponent(editor.getValue())
        }
    });
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Youre using the type post so must use the $_POST in php

PHP

<?php
    if(isset($_POST['admin'])) {
    $admin = $_POST['admin'];
    }
    if(isset($_POST['gname'])) {
    $gname = $_POST['gname'];
    }
?>

Comments

0

You are using $_GET['admin'] which is used to fetch parameters delivered with GET method. You should be using $_POST['admin'] since you have set POST as your http request type in the JS code.

5 Comments

NO, all the code above is in page1.php. The ajax will send the data to page2.php where I use $_POST['admin']. I'm using $_GET['admin'] to get the data that I want to send.
You can try alert to see whether you are getting value of admin variable or not.
@hrs I know its not being sent. I just want to know why is it not working.
Before starting AJAX, below var url; define new variable. var admin = '<?php echo $admin; ?>'; and then alert the admin.
@hrs The alert worked and displayed the variable, but its still not sending.
0

As everyone is saying you're mixing POST and GET. But it caught another thing that may bother you. Are those variables strings? If they are, maybe you're missing the apostrophe/single quote.

<script>
var url = 'page2.php';
$("#save").click( function() {
$.ajax({
   url : url,
    type: 'post',
    data : {
        admin : '<?php echo $admin; ?>',
        gname : '<?php echo $gname; ?>',
        filename : docname,
        content : encodeURIComponent(editor.getValue())
    }
});
});
</script>>

3 Comments

If the "page1.html" is html, it's impossible to get the php variables. Is the page1, the one that creating the javascript code, a php or html?
it's php. The variable working and displaying on page1.php but its not being sent to page2.php for some reason.
Ok. just making sure. I would recommend you to use some tool, like firebug to make sure that variables are being sent. Good luck!
0

Your below code

<?php
    if(isset($_GET['admin'])) {
    $admin = $_GET['admin'];
    }
    if(isset($_GET['gname'])) {
    $gname = $_GET['gname'];
    }
?>

is to be replaced by

    <?php
    if(isset($_POST['admin'])) {
    $admin = $_POST['admin'];
    }
    if(isset($_POST['gname'])) {
    $gname = $_POST['gname'];
    }
?>

Comments

0

You are sending POST parameters,while using get in PHP script. And another issue is you are not using quotes with $admin and $gname variables in ajax request.
Try following :

<script>
var url = 'page2.php';
$("#save").click( function() {
    $.ajax({
        url : url,
        type: 'post',
        data : {
            admin : '<?php echo $admin; ?>',
            gname : '<?php echo $gname; ?>',
            filename : docname,
            content : encodeURIComponent(editor.getValue())

        }
    });
});
</script>

PHP Script:

<?php
    if(isset($_POST['admin'])) {
        $admin = $_POST['admin'];
    }
    if(isset($_POST['gname'])) {
        $gname = $_POST['gname'];
    }
?>

Check if it helps you.

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.