1

I have variable _randNum in JavaScirpt which generate random number between 1 and 50.

I need to select some data from database depending on value of this variable.

I've used following JavaScript code to select data from database via PHP (but without sending JavaScript variable to PHP).

// handles the click event for link 1, sends the query
function getSuccessOutput() {
  getRequest(
      'questions.php', // demo-only URL
       drawOutput,
       drawError
  );
  return false;
}

// handles drawing an error message
function drawError () {
    var container = document.getElementById('output');
    container.innerHTML = 'Bummer: there was an error!';
}
// handles the response, adds the html
function drawOutput(responseText) {
    $.getJSON("questions.php", function(theObject){     

        var d1 = theObject.data1;        // Get the data from PHP
        var d2 = theObject.data2;
}

// helper function for cross-browser request object
function getRequest(url, success, error) {
    var req = false;
    try{
        // most browsers
        req = new XMLHttpRequest();
    } catch (e){
        // IE
        try{
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            // try an older version
            try{
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                return false;
            }
        }
    }
    if (!req) return false;
    if (typeof success != 'function') success = function () {};
    if (typeof error!= 'function') error = function () {};
    req.onreadystatechange = function(){
        if(req .readyState == 4){
            return req.status === 200 ? 
                success(req.responseText) : error(req.status)
            ;
        }
    }
    req.open("GET", url, true);
    req.send(null);
    return req;
}   

How can I send _randNum variable's value to PHP?

In PHP I could do something like that:

$rnd = mysqli_real_escape_string($con, $_POST['randNum']);

But no clue, how to send It from Javascript in same function.

I could create function like this:, but how to use It correctly with getSuccessOutput() function?

function sendFtId() { 
    $.post( "questions.php", { randNum : _randNum })
    .done(function( data ) {        
    });
}

Have you any ideas?

1
  • if your _randNum is available on getSuccessOutput() you can change the url from 'questions.php', to 'questions.php?randNum='+_randNum, note in your php you will use $_GET instead of $_POST Commented Dec 9, 2016 at 12:15

1 Answer 1

1

You can use isset function to check any post request is coming or not. E.g:

function getSuccessOutput($randNum) {
   echo $randNum;
}

if(isset($_POST['randNum'])){
   getSuccessOutput($_POST['randNum']);
}
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.