next Posted December 7, 2012 Share Posted December 7, 2012 I can't seem to figure out how this works... This is my JS: function createRequestObject() { var tmpXmlHttpObject; //depending on what the browser supports, use the right way to create the XMLHttpRequest object if (window.XMLHttpRequest) tmpXmlHttpObject = new XMLHttpRequest(); // Mozilla, Safari would use this method ... else if (window.ActiveXObject) tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP"); // IE would use this method ... return tmpXmlHttpObject; } //call the above function to create the XMLHttpRequest object var http = createRequestObject(); function makeGetRequest() { //make a connection to the server ... specifying that you intend to make a GET request //to the server. Specifiy the page name and the URL parameters to send http.open('get', 'FmPilot validate.php'); //assign a handler for the response http.onreadystatechange = processResponse; //actually send the request to the server http.send(null); } function processResponse() { //check if the response has been received from the server if(http.readyState == 4){ //read and assign the response from the server var response = http.responseText; //do additional parsing of the response, if needed //in this case simply assign the response to the contents of the <div> on the page. document.getElementById('data').innerHTML = response; //If the server returned an error message like a 404 error, that message would be shown within the div tag!!. //So it may be worth doing some basic error before setting the contents of the <div> } } My PHP code: <?php require('simple_html_dom.php'); require('functions.php'); $url = 'http://website'; $username = 'user'; $password = 'pass'; $ch = curl_init(); login($ch, $username, $password, $url); //read CSV file $geninv = getGenInvContent(); echo '<table border="1">'; echo '<th>Invoice </th>'; echo '<th>Work Order</th>'; echo '<th>PO</th>'; echo '<th>Status</th>'; echo '<th>DNE</th>'; echo '<th>Billing Total</th>'; echo '<th>Discrepancy</th>'; echo '<th>Paperwork</th>'; echo '<th>Web Invoice?</th>'; echo '<th>Billed on?</th>'; //loop through it line-by-line foreach($geninv as $invoice) { $dir = 'O:\Accounting Scanning\\'. $invoice['account'] . '\\' . date('Y') . '\\' . $invoice['bulk_invoice']; $result = findBackup($dir, $invoice['invoice']); // collect data off the web regarding current PO $data = getData($ch, $invoice['po_number']); $invoiced_with = isset($data['vendorInvNum']) ? $data['vendorInvNum'] : ''; echo '<tr>'; //if paperwork is found if ($result) { foreach($result as $key=>$signed_workorder) $invoice['backup'.$key] = $signed_workorder; } $difference = round($invoice['total'] - $data['dne'], 2); echo '<td>'.$invoice['invoice'].'</td>'; echo '<td>'.$invoice['workorder'].'</td>'; echo '<td>'.$invoice['po_number'].'</td>'; echo '<td>'.$data['wostatus'].'</td>'; echo '<td>'.$data['dne'].'</td>'; echo '<td>'.$invoice['total'].'</td>'; echo '<td>'. $difference .'</td>'; if ($result) { echo '<td>'; for ($i = 0; $i < count($result); $i++) echo $invoice['backup'.$i].'</br>'; echo '</td>'; unset($result); } else { echo '<td>PAPERWORK NOT FOUND</td>'; } echo '<td>'.$invoiced_with.'</td>'; if ($invoiced_with != '') echo '<td>'.$data['invoicedate'].'</td>'; else echo '<td></td>'; echo '</tr>'; if ($data['wostatus'] <> 'PVINV') { $not_billable[] = array ( 'invoice' => $invoice['invoice'], 'workorder' => $invoice['workorder'], 'po_number' => $invoice['po_number'], 'wostatus' => $data['wostatus'], 'dne' => $data['dne'], 'total' => $invoice['total'] ); } } echo '</table>'; if (isset($not_billable)) exportNB($not_billable); curl_close($ch); ?> My form: <html> <head></head> <script type="text/javascript" src="ajaxrequest.js"></script> <body> <a href="http://localhost/test/FmPilot validate.php" onclick="makeGetRequest()">Check status and DNE</a><br /> <a href="http://localhost/test/FmPilot billing.php">Create Invoices</a> <div id="data"> </div> </body> </html> What I'm trying to do is display all those PHP echoes without reloading, but it isn't working... Looks like AJAX is working, but all it displays is this: '; echo 'Invoice '; echo 'Work Order'; echo 'PO'; echo 'Status'; echo 'DNE'; echo 'Billing Total'; echo 'Discrepancy'; echo 'Paperwork'; echo 'Web Invoice?'; echo 'Billed on?'; //loop through it line-by-line foreach($geninv as $invoice) { $dir = 'O:\Accounting Scanning\\'. $invoice['account'] . '\\' . date('Y') . '\\' . $invoice['bulk_invoice']; $result = findBackup($dir, $invoice['invoice']); // collect data off the web regarding current PO $data = getData($ch, $invoice['po_number']); $invoiced_with = isset($data['vendorInvNum']) ? $data['vendorInvNum'] : ''; echo ''; //if paperwork is found if ($result) { foreach($result as $key=>$signed_workorder) $invoice['backup'.$key] = $signed_workorder; } $difference = round($invoice['total'] - $data['dne'], 2); echo ''.$invoice['invoice'].''; echo ''.$invoice['workorder'].''; echo ''.$invoice['po_number'].''; echo ''.$data['wostatus'].''; echo ''.$data['dne'].''; echo ''.$invoice['total'].''; echo ''. $difference .''; if ($result) { echo ''; for ($i = 0; $i < count($result); $i++) echo $invoice['backup'.$i].' '; echo ''; unset($result); } else { echo 'PAPERWORK NOT FOUND'; } echo ''.$invoiced_with.''; if ($invoiced_with != '') echo ''.$data['invoicedate'].''; else echo ''; echo ''; if ($data['wostatus'] <> 'PVINV') { $not_billable[] = array ( 'invoice' => $invoice['invoice'], 'workorder' => $invoice['workorder'], 'po_number' => $invoice['po_number'], 'wostatus' => $data['wostatus'], 'dne' => $data['dne'], 'total' => $invoice['total'] ); } } echo ''; if (isset($not_billable)) exportNB($not_billable); curl_close($ch); ?> it's a bit of my PHP code. Then, a few seconds later the page refreshes and I get my normal values from php. How do I get it to work the way I need? Quote Link to comment https://forums.phpfreaks.com/topic/271721-display-echo-output/ Share on other sites More sharing options...
next Posted December 7, 2012 Author Share Posted December 7, 2012 Why am I not able to edit my posts? Forget the above code, might seem like too much code. Here's a more basic example: js: function createRequestObject() { var tmpXmlHttpObject; //depending on what the browser supports, use the right way to create the XMLHttpRequest object if (window.XMLHttpRequest) tmpXmlHttpObject = new XMLHttpRequest(); // Mozilla, Safari would use this method ... else if (window.ActiveXObject) tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP"); // IE would use this method ... return tmpXmlHttpObject; } //call the above function to create the XMLHttpRequest object var http = createRequestObject(); function makeGetRequest() { //make a connection to the server ... specifying that you intend to make a GET request //to the server. Specifiy the page name and the URL parameters to send http.open('GET', 'http://localhost/test/test.php', true); //assign a handler for the response http.onreadystatechange = processResponse; //actually send the request to the server http.send(null); } function processResponse() { //check if the response has been received from the server if(http.readyState == 4){ //read and assign the response from the server var response = http.responseText; //do additional parsing of the response, if needed //in this case simply assign the response to the contents of the <div> on the page. document.getElementById('data').innerHTML = response; //If the server returned an error message like a 404 error, that message would be shown within the div tag!!. //So it may be worth doing some basic error before setting the contents of the <div> } } PHP: <?php echo 'worked'; ?> HTML <html> <head></head> <script type="text/javascript" src="ajaxrequest.js"></script> <body> <a href="#" onclick="makeGetRequest()">Check status and DNE</a><br /> <!-- <a href="http://localhost/test/FmPilot%20validate.php" onclick="makeGetRequest()">Check status and DNE</a><br /> <a href="http://localhost/test/FmPilot billing.php">Create Invoices</a> --> <div id="data"> old text </div> </body> </html> It isn't working and I don't understand why. Quote Link to comment https://forums.phpfreaks.com/topic/271721-display-echo-output/#findComment-1398128 Share on other sites More sharing options...
requinix Posted December 7, 2012 Share Posted December 7, 2012 You can edit but there's only a short window after posting in which you can. For some reason the PHP code is not executing. So that's where to start looking. But you say that the AJAX works the second time? Or that the AJAX doesn't work at all and something else is refreshing the page? Quote Link to comment https://forums.phpfreaks.com/topic/271721-display-echo-output/#findComment-1398158 Share on other sites More sharing options...
next Posted December 7, 2012 Author Share Posted December 7, 2012 Nah, it's the AJAX that doesn't work. I was originally getting some data because of the URL I used, it was incorrect. It's strange, responseText has no value in it, but when I look at the headers, something is being transmitted with the same number of characters as my PHP echo string. So if I change echo to 8 character word, I see that 8 characters are passed to my html page, but for whatever reason I can't see responseText. Quote Link to comment https://forums.phpfreaks.com/topic/271721-display-echo-output/#findComment-1398167 Share on other sites More sharing options...
DavidAM Posted December 7, 2012 Share Posted December 7, 2012 http.open('get', 'FmPilot validate.php'); Since you are not sending any parameters, you can easily type that address into your browser and see what you are getting back. Be sure to use the "View Source" feature of the browser, in case the returned data contains HTML markup. I see there is a space in that url. I have always avoided that. I don't know if that is a problem or not. Also, that url is not qualified (no scheme or domain) so the browser is sending it relative to the page you are on. You'll have to provide the full url for the test mentioned above; and probably should provide a full url for the AJAX request as well. Oops, I see you changed it in your most recent post: http.open('GET', 'http://localhost/test/test.php', true); Try typing that, "http://localhost/test/test.php", directly in the browser and see what comes back. Also, what headers are you getting? Some of them may be pertinent to the problem (i.e. cacheing, etc). Quote Link to comment https://forums.phpfreaks.com/topic/271721-display-echo-output/#findComment-1398173 Share on other sites More sharing options...
next Posted December 7, 2012 Author Share Posted December 7, 2012 Found the problem. I was opening the html file locally (file///C:/blah blah blah), instead of through localhost. For whatever reason it's breaking ajax. Quote Link to comment https://forums.phpfreaks.com/topic/271721-display-echo-output/#findComment-1398179 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.