Jump to content

Recommended Posts

How would I implement a javascript confirm box within a PHP if statment? I have an if statement, and if it is true, I want a confirm box to pop up asking the user if they would like continue, or cancel the operation and go back. If they click yes, it should continue with the rest of the script. If no, it will simpily redirect back to the first page. If the if statement is false all together, it just skips that step and moves on to the next else statment. I have the entire php script working, it's just the confirm box I want to implement so the user doesn't unexpectedly have something happen they don't want to happen. Otherwise, the script will just continue without asking the user anything, and they might not want that.

I suggest you focus on this part as you describe first:

I want a confirm box to pop up asking the user if they would like continue, or cancel the operation and go back. If they click yes, it should continue with the rest of the script. If no, it will simpily redirect back to the first page.

Search the web for javascript confirm box and let us know if you get stuck with your code.

you cannot mix php and javascript like that.  php is a server-side language, it gets parsed on the server and after it is done, the results are output, sent to the browser.  From there, your javascript will be executed, because javascript is a client-side language, parsed and executed on the client (in this case, the browser).  As far as php (and your server) is concerned, javascript is just arbitrary text.

As Crayon Violent says, javascript is only executed on the browser, but you can output a javascript confirmation box as any other html content. You can create another php page to handle the the confirmation, but you can't start loading the php code and in a certain time stop it to ask for something, it loads, outputs and finishes.

Ok, thanks. So I'd do something like if the statement is true, redirect to this new page. On the new page, I would have the confirm box. If they say yes, it continues with the code on the new page. If no, it redirects back to another page. If the statement is false overall, it just moves on.

 

Would that work?

Yes, if you do this in separated pages it woul work. There's no need of javascript for what you are saying, just a form that will be submitted each time you click the submit button and it will be checked in the next page if what was inputted was correctly. (You don't need javascript unless you want to make something else than a submit button to change pages).

It will be like a normal form.

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.