I ran into an issue for which I cannot find the answer, I rarely ask questions here, but I am rather stumped. Any assistance shall be appreciated.
This is the PHP that receives the Ajax call.
<?php
session_start();
$_SESSION["my_data"] = $_POST['action'];
$DB_HOSTNAME = 'localhost';
$DB_USERNAME = 'username';
$DB_PASSWORD = 'password';
$link2 = mysqli_connect($DB_HOSTNAME,$DB_USERNAME,$DB_PASSWORD) or die('Unable to establish a DB1 connection');
mysqli_select_db($link2, '$DB_USERNAME');
$orderQuery = mysqli_query($link2, "SELECT * FROM table WHERE id='".$_SESSION['my_data']."'");
$orderQuery = mysqli_fetch_assoc($orderQuery);
$orderInfo = "
<table class='table table-striped'>
<tbody>
<tr>
<td>#: </td>
<td>". $_SESSION['my_data'] ."</td>
</tr>
<tr>
<td> Full name: </td>
<td>". $orderQuery['firstname'] . " " . $orderQuery['lastname'] ."</td>
</tr>
<tr>
<td> Address: </td>
<td> ". $orderQuery['shipping_address_1'] ."<br> ". $orderQuery['shipping_city'] . " " . $orderQuery['shipping_zone'] . " " . $orderQuery['shipping_postcode'] ." </td>
</tr>
<tr>
<td> Card Expiry Date Month: </td>
<td> 08 </td>
</tr>
</tbody>
</table><br>
";
echo $orderInfo/* . $_POST['action']*/; ?>
And this is the script that makes the call.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var myWindow;
function myFunction() {
myWindow = window.open('', '_blank');
myWindow.document.write("<link rel='stylesheet' type='text/css' href='stylesheet.css'>");
var orderNum;
orderNum = document.getElementsByClassName('summary_value')[0].innerHTML;
orderNum = orderNum.replace("#", "");
$.ajax( { type : 'POST',
data : {'action':orderNum},
url : 'process.php',
success: function ( data ) {
myWindow.document.write( data );
},
error: function ( xhr ) {
alert( "error" );
}
});
myWindow.document.write("<br>");
myWindow.document.write(document.getElementById("payInfor").innerHTML);
}
</script>
<button onclick='myFunction()' class="btn btn-default">Print Pay Info</button>
It could be a simple issue, but I can't see it.
I know the Ajax is working because it displays some of the information ($_SESSION['my_data']), so I am thinking it's something with my SQL statements, but the syntax looks correct.
mysqli_select_db($link2, '$DB_USERNAME');when you have singlequtotes, it'll not be a variable, but that exact string instead. Then, enable error-reporting for PHP and check the console for any JS errors, get MySQLi to tell you about any errors there usingmysqli_error()(or set MySQLi to throw exceptions). Debugging by getting the errors the script throws back at you is always the first stop! Then you should use parameterized queries, you already have an API that supports it.