Jump to content

Confirm Message Box - Php/Javascript


xxxpeanutbutter

Recommended Posts

hey guys, im pretty awful in programming, so bare with me.

 

im using javascript to pop-up a message box.

if user confirms, the page will insert data in mysql and redirects to the index page.

if user cancels the pop-up message, it will just stay on the main page.

 

but i can't seemed to pass the variables as it calls a set function...

 

what did i do wrong? any help from anyone is highly appreciated.

 

html page

<form method="post" action="seatplan_occupied.php">
<input name = "btn_seatOccupied" type = "submit" class="btnFORM" value="CONFIRM" onClick="seatOccupied()">
</form>

php/javascript page (seatplan_occupied.php)

<script>
function seatOccupied()
{
	document.getElementById("btn_seatOccupied").innerHTML;
    
    if (confirm("Are you sure you want to confirm your purchase?") == true)
	{
		alert("Yeay! You bought ticket/s");
		<?php
		include("databaseconnect.php");

		mysql_query("UPDATE tblseatchart SET seatSTATUS='Occupied' WHERE seatSTATUS='Selected'");

		include("index.php");
		?>
	}
	
	else
	{
       alert("Owh No! You Cancelled to confirm... Whats wrong?");
    }
    
}
</script>
Link to comment
Share on other sites

You can't embed php code inside a js function.  You need to let your js function return true/false to your submit button and that will determine if your submit calls the php script that will do the update

 

 

<input name = "btn_seatOccupied" type = "submit" class="btnFORM" value="CONFIRM" onClick="return seatOccupied()" action='postdata.php'>

 

Note the addition of 'return' - this will either submit the form and call the script to do the update for you.  Or not.

Link to comment
Share on other sites

The main problem is that you don't seem to understand when PHP code and JavaScript code is actually executed.

 

PHP scripts run on the server. They are invoked by your webserver to produce the HTML document (or any other content). This document is sent to the client. And then the browser runs the JavaScript code which is embedded into the HTML document. So PHP and JavaScript are executed at two entirely different points of time by two entirely different agents. They are not even aware of each other.

 

That means trying to invoke the PHP interpreter within JavaScript is not only impossible, it doesn't even make sense. You're confusing two different layers. The problem is that your programming style actually fosters this: You just throw all PHPSQLHTMLCSSJavaScript into one big file with no structure whatsoever. No wonder you've lost track of which code runs when.

 

You should avoid this so-called spaghetti code and cleanly separate the different languages: JavaScript code belongs into external scripts, do not stuff it into inline scripts or attributes within the HTML document. The same goes for CSS. PHP code also doesn't belong into HTML markup except when its only purpose is to generate the HTML. A good rule for beginners is: Put all PHP code on top and all HTML markup to the bottom of the script.

 

Going back to your question, there are two options:

  • You send the form to the user. When they try to submit it, they are stopped by a JavaScript function which asks them for confirmation. If they confirm their action, the JavaScript function adds some additional form field and lets the browser submit the form. Otherwise, the form is not submitted. The target PHP script then checks the presence of the extra field: If it's there, you can add the data. Otherwise, the user doesn't have JavaScript running and needs to do a confirmation with plain HTML (another form, a checkbox or whatever).
  • You use Ajax to trigger the target PHP script with your JavaScript function. I don't recommend this, because it's much more complex and requires advanced knowledge.

ginerjm's suggestion is not a good idea, because if the user has turned off JavaScript (like me), the browser just submits the form without asking for confirmation first.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.