#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    4
    Rep Power
    0

    Javascript / PHP problem


    I get a value from a JS confirm box, this returns fine as either true or false, I know this because I echo it. But when I try do an IF statement it does not seem to recognize it:
    PHP Code:
    echo ("<script>var conf = confirm('Are you sure you want to delete this item?');</script>");
    echo (
    "<script language=JavaScript> document.write(conf);</script>");
    $confirm "<script language=JavaScript> document.write(conf);</script>";    
    if (
    $confirm =="true")  
    {  
    blah etc 
    It must be a really simple error, thanks in advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    400
    Rep Power
    12
    take away the double qoute for true or you can try

    if ( !empty($confirm) )
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    4
    Rep Power
    0
    Guh neither method works, I had already tried removing the quotes :/

    Any other ideas?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    400
    Rep Power
    12
    how come? its working for me though

    PHP Code:

    $confirm 
    "<script language=JavaScript> document.write(conf);</script>";    
    if ( !empty( 
    $confirm ) )
    {  
       echo 
    "hello";

  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    4
    Rep Power
    0
    That code prints hello no matter what button you click on the confirmation box for me, same with my code, it deletes from the db whether I click cancel or ok.
  10. #6
  11. Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    5
    Rep Power
    0

    server vs client side


    I think your dealing with server vs client side issues here.

    $confirm = "<script language=JavaScript> document.write(conf);</script>";
    if ( !empty( $confirm ) )
    {
    echo "hello";
    }
    The value of $confirm is getting set to "script language=JavaScript> document.write(conf);</script>" so it is not empty and will always return true and execute your if statement.

    You are building a script to exectute at the client side.
    once the client side script is executed (your confirmation box) then you have to pass the value BACK to the server to execute a server side script on it.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    4
    Rep Power
    0
    How would I go about that?

    I can get the box to pass values into php though, instead of echoing the above code, I used an include conf.html which returned true or false.

    But the If statement still wouldn't process it.

    Should I just scrap the idea of a conf box and rewrite my code to do it solely in php?
  14. #8
  15. No Profile Picture
    Dissident
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2003
    Location
    New York
    Posts
    1,671
    Rep Power
    49
    It depends what you are trying to do.

    It sounds like you want to get input from a user (via a confirmation box) and pass it back to your script. PHP doesn't work like that though. Php code is compiled server side. After the entire script is finished parsing, it takes the results and sends them to the user. By the time your user is clicking on that confirmation box, your script is long done running and the server has forgotten all about it.

    You have two basic solutions to your problem.

    1) You can have your script write out a huge chunk of javascript so that when your user clicks on the box, the javascript will determine all of the rest of the decisions that need to be made.

    I don't suggest this course of action for several reasons. First and foremost, having code write other code is usually a messy affair fraught with bugs. Secondly, it will send a lot of information to the user that they just don't need to have.

    2) The other solution is to take the result of that confirm box and send it back to the server. Then you can do whatever you want in php and send a new page to your user.
  16. #9
  17. Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    5
    Rep Power
    0
    You could throw the confirmation result into a variable or set it to the value of a hidden form field and use a bit of client side javascript to send it back to the server using GET or POST for processing.

    It adds a bit of complexity to the process but it can be done.
    I've used this method many times in ASP.

IMN logo majestic logo threadwatch logo seochat tools logo