Re: [SMARTY] yes/no dialog

[email protected] (Ken Snyder) Tue, 24 Oct 2006 16:55:39 -0600
Newsgroups php.smarty.general
Message-ID <[email protected]>
Hello Michael,

You've just discovered the perils of programming in a stateless 
environment.  :P  Generally, the way to overcome the limitations of 
stateless environments is to store data in php's $_SESSION superglobal 
array ({$smarty.session} in smarty).  Also, in the web 2.0 environment, 
Ajax is also used often.

For this situation, however, something simpler may work for you:



<?php
if( isset($_POST['request']) || isset($_POST['i_am_sure']) ) {
    if( $willBeOverwritten==false || isset($_POST['i_am_sure']) ) {
        // do sql
    } else {
        $smarty->assign('ask_if_sure',true);
    }
}
?>
...
<form action="{$smarty.server.PHP_SELF}" method="post">
Requested Operation:
<input type="text" name="requested_operation" 
value="{$smarty.post.requested_operation}" />
{if $ask_if_sure}
    Do you really want to overwrite?
    <input type="submit" name="i_am_sure" value="Yes" />
    <input type="submit" name="cancel" value="No" />
{else}
    <input type="submit" name="request" value="Do It" />
{/if}
</form>



Basically php checks of data would be overwritten, and if so, loads the 
page again with all the data as the user just filled out, but asks "Are 
you sure." Then, once the request is submitted again (using the 
"i_am_sure" submit button), the script will go ahead and overwrite the data.

--Ken


Michael Kolakowski wrote:
> Hi,
>
> I am new to both PHP and Smarty.  Also new to JavaScript, for that 
> matter. ;)  Anyhow, I am trying to create a yes/no dialog and am 
> having a lot of trouble.  Could I please get some help?
>
> My php code looks a little like this:
>
> if($variable != NULL)
> {
>   //do a sql insert statement here
> }
> else
> {
>   //ask user if they really want to 'overwrite'    <-- yes/no goes here!!
>  
>   //if yes, then do sql update statement
>   //else, do nothing
> }
>
> So, I want the yes/no dialog to pop up in that else block.  Using 
> JavaScript was suggested, but I have not been able to figure out how 
> to get the value of the yes/no (I got the pop-up by using a php print 
> statement).  In addition, I have a number of variables above the if 
> block that are used for the sql statements.  I don't want to 'lose' 
> these variable due to POST or whatever else.  I'd also like to avoid 
> complex logic to pass them around between the template and php files.  
> Ideas?
>
> Thanks,
> Michael Kolakowski
> HPC-1
>