John
John

Reputation: 11

Passing a PHP variable using ajax


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.

Upvotes: 1

Views: 106

Answers (6)

Nishu Tayal
Nishu Tayal

Reputation: 20880

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.

Upvotes: 0

user3989103
user3989103

Reputation:

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>

Upvotes: 3

varad mayee
varad mayee

Reputation: 619

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'];
    }
?>

Upvotes: 0

rGonzalez
rGonzalez

Reputation: 26

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>>

Upvotes: 0

I&#39;m Geeker
I&#39;m Geeker

Reputation: 4637

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'];
    }
?>

Upvotes: 1

Juuso Elo-Rauta
Juuso Elo-Rauta

Reputation: 29

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.

Upvotes: 0

Related Questions