PHP script embedded in js function running when not called

hey guys, I’m not understanding something that’s going on here. I have PHP code in 2 parts of my page. 1 script to echo out traffic data, and a 2nd script embedded inside a js function that is called when a button is clicked. The first time I visit the page, the data echoes out just fine, however if I reload the page by pressing the “reload” button on the browser’s address bar, the 2nd script inside the js function is apparently executing cuz there is nothing echoing out, indicating that the database table is empty. Here is the HTML for my button:

<input type="button" onclick="emptyReport();" id="Button1" name="button" value="Clear Report" style="position:absolute;left:1px;top:1px;width:96px;height:25px;font-family:Arial;font-size:13px;z-index:5">

and this is the js function that is causing the problem:

< script type=“text/javascript” >

function emptyReport() {
if (confirm(‘Are you sure you want to clear the report data?’)) {

	<?php
	$dbHost = "localhost";
	$dbName= "rptDatabase";
	$dbUsername = "username";
	$dbPassword = "password";
	
	$conn = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName);
    	//throw the old report data into a log history table
    	mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
    	$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

    	//Add current traffic data to a history log table
    	$stmt = $conn->prepare("INSERT INTO tblTrafficHistory (ip, host, page, date, time)
    						  	SELECT ip, host, page, date, time FROM tblTraffic");
    	$stmt->execute();

    	//Delete traffic data from display report and start fresh
    	$stmt = $conn->prepare("DELETE FROM tblTraffic");
    	$stmt->execute();

    	$stmt->close();
    	$conn->close();
    	?>
    	
    	//reload the page to show that the report data has been cleared
        alert('Report data has been cleared!  You will now only see visitor information from this point on.');
        header("Refresh:0");
} else {
}

}
< /script >

can someone tell me my error?

There’s no such thing as php code embedded inside of javascript. All the php code on a page is executed on the server when the page is requested, since php is a server-side scripting language. Javascript is executed in the browser, since the javascrpt you are using is a client-side scripting language. The only thing javascript can do to communicate to the server is make either a post method or a get method http request to the server. Perhaps you want to use AJAX to make an Asynchronous post/get method http request from javascript to the server?

ok i mite have to do that. i’ll look it up and get back to u. thanks!

phdr,

I tried to implement something i found on w3schools, but i’m not seeing the confirm dialog popup. can you give me a push? here’s what i’ve got:

< script >
function emptyReport() {
if (confirm(‘Are you sure you want to clear the report data?’)) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(“GET”, "deletereportdata.php?);
xmlhttp.send();

    	//reload the page to show that the report data has been cleared
        alert( 'Report data has been cleared!  You will now only see visitor information from this point on.' );
        header( "Refresh:0" );
	} else {
	}
}

< /script >

Perhaps if you edit your post and use either bbcode [code][/code] tags or the forum’s preformatted text </> menu button, so that the code will appear as is and can be read?

  `  <script>`
`function emptyReport() {`
`if (confirm(‘Are you sure you want to clear the report data?’)) {`
`var xmlhttp = new XMLHttpRequest();`
`xmlhttp.open(“GET”, "deletereportdata.php);`
`xmlhttp.send();`

`    	//reload the page to show that the report data has been cleared`
`        alert( 'Report data has been cleared!  You will now only see visitor information from this point on.' );`
`        header( "Refresh:0" );`
`	} else {`
`	}`
`}`
`</script>`

hey, I don’t think the < / > option on this forum works man. everytime I push that button when I have the code highlighted, it only puts single quote marks around the first line. I had to highlight each line at a time in that last post to get the code to appear on the page properly. is this a bug on this forum?

i might be in the wrong place, phdr. the code i posted is js and this is a PHP forum. so I don’t expect u to respond. sorry about that.

The <input ... line from your first post and the last posted javascript (after undoing the forum’s ‘publishing’ alterations - [code][/code] tags would have been a better choice to use) works for me. What does your browser’s developer console say?

BTW - you should use a post method request when performing an action on the server, such as creating, updating, or deleting data. This is so that a search engine indexing your site won’t cause unexpected changes.

there are “?” marks around the CONFIRM() function and the GET() method in PHP. see image I’ve posted:

1

Those are smart/curly-quotes, the result of ‘publishing’ code. You must delete and re-type them as straight, plain, ascii quotes.

ok man, here’s what I have:

<script>
function emptyReport() {
    if (confirm('Are you sure you want to clear the report data?')) {
        var xmlhttp = new XMLHttpRequest ();
        xmlhttp.open("GET", "deletereportdata.php");
       xmlhttp.send();

       alert('Report data has been cleared! You will now only see visitor information from this point on.');
       header("Refresh:0");
    }
}
</script>

the dialogs are working perfectly now, however the page is not reloading even with the “header()” function in there. isn’t header() a PHP function though? if I include “?” tags around that line and go to the page, it reloads constantly and won’t stop. which of course makes good sense, as i realize that php is executed before anything else like u said.

Header is a PHP function, it is not a Javascript function however. PHP doesn’t do refresh.

sorry for the late reply, astone. how exactly would I make the page reload itself with PHP then? I got the code from my last post thru a goog search.

I don’t see the point of using Ajax if you want the page to refresh, that is the entire point of using ajax. If that’s what you want to do, just have the page submit and it will cause it to reload by default.

You can use javascript for a refresh, however.

location.reload();

as in SUMBIT, you mean like the following without any value in the METHOD arg?

<button type="submit">

then clicking the button will simply refresh the page ur saying?

Well the method type attribute for the form is still needed to the backend knows how to process the message. But, yes. If you just submit the form, it will do the refresh on it’s own.

Maybe I am misunderstanding what you want? Are you just looking to clear fields? Or does it also need to do something on the backend as well?

this is just a simple-stupid traffic report for a small business man. so what i’ve done is echoe’d out all the database data that’s captured on every page of his website for every visitor. I’ve filtered the queries though with a WHERE clause so he doesn’t see all the bot visits from the various companies that do that crap.

so I’ve got a 5 column HTML table and PHP echo’s out all the data in it, and above the table I’ve got a plain button that says “clear report”. when he clicks it, what it should do is throw the data that’s in the table to a new table where the rest of the prior historical traffic data is stored, delete the table data, show a javascript message that tells him he’s starting fresh, then reload the page to show a blank screen that tells him there’s no records to show.

I had it working at one point, but somehow screwed it up. I have the js code to show the message, but when I put PHP tags around the DELETE statement in SQL, the js alert function before the tags does not execute , nor does the confirm() function that appears before the tags.

Okay, so there is backend processing that needs to run then.

I would still tackle this in a non-ajax version. let the button do its’ default behavior, it will hit the php script for doing all that processing, causing the truncated table to be fetched with not data, and the process restarts.

i will test it and get back to you. I never did think that AJAX was needed for this, but someone who obviously was mistaken told me differently. thanks!

Sponsor our Newsletter | Privacy Policy | Terms of Service