Jump to content

Confirmation Script Help


Selacius

Recommended Posts

I am using the script which pops up a confirmation box to allow my users to select which quests they would like to partake in. Depending on what button is pressed, the user will either accept the quest, or decline it. If the user accepts the quest then I must modify the database with some new values to reflect the user accepting the quest. This doesn't work though because the values get updated in the database regardless of whether the user actually accepts the or declines the quest although the code for the mysql_query is found in the if ( confirm("Text Here") ) { portion of the script. This is how I have setup the confirmation script to work with the mysql_query, maybe I am somehow doing it wrong.

<script>if ( confirm("Conditional Question To Start Quest") ) {document.write ("Comment about accepting quest.");document.write ("<font color=black><?=mysql_db_query ("$dbx", "UPDATE **** SET `*` = '*' WHERE * = '*'"); ?></font>");}else {document.write ("Comment about declining quest.");}</script>

Now I have to use the font color command in the query because it will display a value of 1 which looks pretty unsightly. I'm sure that there is more likely a way to do this with AJAX but I am still trying to learn javascript. If someone could make me a script which will have the confirmation box, areas to modify for text depending on the action the user has taken and also the ability to properly modify the db when the quest is accepted, it would be much appreciated. Or at least show me how I would go about doing so.

Link to comment
Share on other sites

you'll need to update this piece to

<?=mysql_db_query (\"$dbx\", \"UPDATE **** SET `*` = '*' WHERE * = '*'\"); ?>

You are trying to use double quotes inside double quotes...you can't unless you escape them.

Link to comment
Share on other sites

With those changes, will it make it so that I only update the database when the player accepts the quest? Because it has no problem modifying the database, its just the fact that it does so regardless of the users selection.

Link to comment
Share on other sites

I've found that javascript handles errors very poorly. A perfect example of this is that (in 100% of my experiences, at least), when javascript encounters an error of some kind it ceases running (obviously), BUT if it's running a function of any kind it always returns true, and this may be part of the problem. I'm guessing one of the following is happening:1. There's some kind of error inside the dialog box (dunno why, but it would explain why the database is always updating)2. The script is being terminated/escaped at some point before the snippet of PHP, and the browser interprets what SHOULD be javascript as HTML, bypassing your conditional statementI dunno about IE, but I know Firefox has some good javascript debugging features and extensions...you should thoroughly test and examine your code in action (alert boxes are your friend :) ) and you should be able to pin down what's happening fairly quickly.

Link to comment
Share on other sites

I don't think its the code itself because it correctly displays the pop-up window, and withholds the text from being displayed until the proper selection is made. The code itself works exactly as I would like, but the problem is in the mysql_query updating the database regardless of the user's selection.

Link to comment
Share on other sites

Of course it is not working. You can't combine JS and PHP just like that. PHP will execute the mysql_db_query before any user input, since the browser will never see anyway the query, you are only echoing the results. The PHP will parse the script, execute the query, generate the HTML file and send it to the browser.Better try separate pages something like:

<script>if ( confirm("Conditional Question To Start Quest") ) {document.location='script_with_mysql_update.php';}</script>

Link to comment
Share on other sites

If you don't care about reloading the same page you might try something like:

<?phpif (!isset($_GET['confirmed'])) {?><script>if ( confirm("Conditional Question To Start Quest") )document.location='?confirmed=true';elsedocument.write ("Comment about declining quest.");</script><?php} else {mysql_db_query(...);echo "Comment about accepting quest.";}?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...