1

i'm new to web and facing issue while using PHP file's constants inside javascript Ajax call.

My PHP code in constants.php file is as:

<?php

$color = 'green';
define ('BASE_URL', 'https://example.com?');
define ('APP_KEY', 'abcde');
define ('USER_KEY', '12345');
?>

My Ajax call in another login.php file is as:

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {

$("#loginPopup").on('click',function(){

    var x = document.forms["login"]["emailId"].value;
    var pwd = document.forms["login"]["pwd"].value;

    $.ajax({
    type: 'GET',
    url: 'BaseURL?appkey=abcde&userkey=12345&email='+ x +'&password=' + pwd,
    crossDomain: true,
    dataType: 'jsonp',

    success: function (response) {
        showAlert(response);
    },
    error: function (request, status, error) {
        alert("ERROR");
    }
});
});
});
</script>

I want to move constants i.e. Base URL, Keys etc to constants file. So i've created constants.php. But now, i don't know how to use that inside ajax call. Please help. Thanks in anticipation.

1
  • 1
    Change This url: ' <?php echo BaseURL ?>?appkey=abcde&userkey=12345&email='+ x +'&password=' + pwd, Also needs to include constant file in login.php Commented Aug 21, 2017 at 11:10

1 Answer 1

4

Like so, assuming the two files are in the same dir:

<?php
require_once('constants.php');
?>

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {

$("#loginPopup").on('click',function(){

    var x = document.forms["login"]["emailId"].value;
    var pwd = document.forms["login"]["pwd"].value;

    $.ajax({
    type: 'GET',
    url: '<?php echo BASE_URL  ?>?appkey=<?php echo APP_KEY  ?>&userkey=<?php echo USER_KEY  ?>&email='+ x +'&password=' + pwd,
    crossDomain: true,
    dataType: 'jsonp',

    success: function (response) {
        showAlert(response);
    },
    error: function (request, status, error) {
        alert("ERROR");
    }
});
});
});
</script>
Sign up to request clarification or add additional context in comments.

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.