Unable To Access Ajax Data [php] November 15, 2024 Post a Comment 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 likeecho'http://example.com/' . $_POST['idgen']; die(); CopyIf you want php to do redirect you shouldn't use Ajax. Non ajax method, you almost got it right <formaction="run.php"method="POST"target="_blank"><inputtype="hidden"id="idgen"name="idgen"value="<?phpecho$random?>"><inputtype="text"name="userinput"id="userinput"><buttontype="submit">Go!</button></form>CopyThe above code will submit to run.php directly, and you can use your original header() function to do redirect.in phpBaca Juga500 Internal Server Error On Xhr RequestAngularjs Not Allowing Square Brackets In The Url Parameter - '['Ajax Function Working On Local But Not Onlineheader('Location: http://example.com/'.$_POST['idgen']); CopyUpdate/* RUN.PHP PAGE - THE PAGE TO RECEIVE THE JAVASCRIPT RESULTS */<?php$endurl = $_POST['idgen']; ?><?phpif (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(); } elseif (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(); } ?>CopyThen in javascript you need to check for falsefunctioncalcResult() { var userinput = document.getElementById('userinput').value; var randomid = document.getElementById('idgen').value; Copy// various functions $.ajax({ type: "POST", url: 'http://example.com/' + randomid, // same random url as the form sends todata: { result: "Hello!", userinput: userinput, idgen: randomid }, // using simple result for testing purposessuccess: function(response){ if (response != 'false') { window.location.replace(response); } else { alert('error encountered'); } } Copy}); Share You may like these postsUsing Php In .js File? Post a Comment for "Unable To Access Ajax Data [php]"
Post a Comment for "Unable To Access Ajax Data [php]"