Skip to content Skip to sidebar Skip to footer

Unable To Access AJAX Data [PHP]

A form on my homepage (index.php) opens a randomly generated URL in a new tab, after being submitted. That random url is running a script called run.php

Solution 1:

you can try windows.location of JavaScript and pass your value instead of header location.


Solution 2:

In php u echo the URL to redirect to like

echo 'http://example.com/' . $_POST['idgen'];
die();

If you want php to do redirect you shouldn't use Ajax.

Non ajax method, you almost got it right

<form action="run.php" method="POST" target="_blank">
    <input type="hidden" id="idgen" name="idgen" value="<?php echo $random ?>">
    <input type="text" name="userinput" id="userinput">
    <button type="submit">Go!</button>
</form>

The above code will submit to run.php directly, and you can use your original header() function to do redirect.

in php

header('Location: http://example.com/'.$_POST['idgen']);

Update

/* RUN.PHP PAGE - THE PAGE TO RECEIVE THE JAVASCRIPT RESULTS */

<?php

    $endurl = $_POST['idgen']; ?>

    <?php
    if (isset($_POST['userinput'])) {

  // DATABASE LOGIN DETAILS HERE
        $conn = new mysqli($servername, $username, $password, $dbname);

        if ($conn->connect_error) {
            echo 'false';
            die();
            //die("Connection failed: " . $conn->connect_error);

            //Just echo False so Javascript knows what's happening.
        }

        $sql = "INSERT INTO mydatabasename (userinput,randurl)
        VALUES ('$_POST[userinput]','$_POST[idgen]')";

        if ($conn->query($sql) === TRUE) {
            //echo "New record created successfully"; // You cannot echo anything else if you want to do a redirect. Try error_log?
            error_log('New Record Created Successfully');
        } else {
            echo 'false';
            die();
            //echo "Error: " . $sql . "<br>" . $conn->error;
            //let javascript know you failed.

        }

        $conn->close();
           // header('Location: http://example.com/'.$endurl);
           echo 'http://www.example.com/'.$endurl;
           die();
    }
    else if (isset($_GET['idgen'])) {
      // DATABASE LOGIN DETAILS HERE
        $conn = new mysqli($servername, $username, $password, $dbname);

        if ($conn->connect_error) {

            //die("Connection failed: " . $conn->connect_error);
            error_log('Connection Failed:' . $conn->connect_error );
            echo 'false';
            die;
        }

        $sql = "SELECT userinput FROM mydatabasename WHERE randurl = '".$_GET['idgen']."'";

        $result = $conn->query($sql);

        if ($result) {
                if ($row = $result->fetch_array()) {

            }
            $result->close();
      }
      $conn->close();
    }
    ?>

Then in javascript you need to check for false

function calcResult() {
  var userinput = document.getElementById('userinput').value;
  var randomid = document.getElementById('idgen').value;

// various functions

$.ajax({
        type: "POST",
        url: 'http://example.com/' + randomid, // same random url as the form sends to
    data: {
    result: "Hello!",
    userinput: userinput,
    idgen: randomid

    }, // using simple result for testing purposes
    success: function(response){
        if (response != 'false') {
        window.location.replace(response);
        } else {
        alert('error encountered');
        }
   }

});


Post a Comment for "Unable To Access AJAX Data [PHP]"