Results 1 to 39 of 39

Thread: Redirection using JavaScript within PHP

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Redirection using JavaScript within PHP

    OK, my major hold up here is not getting the format rules of PHP. I don't get when I need a backslash and when I don't. The rules just don't make sense to me. What I'm trying to do is send the user back to the login page if their input is not accepted. If it is accepted, I want to send them to the guest book. I could probably write a function and then in the conditional statement just send the right page to the redirect function.

    PHP Code:
    <BODY>

      <?PHP

        $db 
    = @mysql_connect("connection""root""password") or die("Could not establish connection with database");

        
    mysql_select_db('guestbook');

        
    $query "INSERT INTO Guests(UserName, Email, Comments, Date) Values('$UserName', '$Email', '$Comments', '".date("Y-m-d")."')";

        
    $result =  mysql_query($query) or die("Unable to enter your information.  Please try again later.");

        if 
    $result
          
    {
            <
    SCRIPT LANGUAGE="javascript">
              
    document.location.href='test.php'
            
    </script>
           }
        else
          {
            <
    SCRIPT LANGUAGE="javascript">
              
    document.location.href='login.php'
            
    </script>
           }

      
    ?>

    </BODY>
    That didn't work. I'm sure it's just syntax stuff.

  2. #2
    Addicted Member Martin Wilson's Avatar
    Join Date
    Mar 2002
    Location
    :)
    Posts
    236
    Try:
    Code:
    if $result
          {
            ?><SCRIPT LANGUAGE="javascript">
              document.location.href='test.php'
            </script><?
           }
        else
          {
            ?><SCRIPT LANGUAGE="javascript">
              document.location.href='login.php'
            </script><?
           }
    What is the answer to this question?

  3. #3

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    OK, I'll give it a shot. Is that code basically escaping back into html?

  4. #4

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I couldn't get it to work. I pasted the exact code you put in your post.

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Use PHP Headers to redirect instead?

    PHP Code:
    header ("Location: http://www.yoursite.com/test.php"); 
    However, the header() function has to be called before any output is sent. So you can't do this after anything has been outputted to the browser.

    If you need more info on what I mean, just ask.

    Hope this helps.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    can you put that code in context please? I have no idea what to do with it. I need a conditional. If their information is successfully added to the database, then they get redirected to the guest book . Otherwise, they are taken back to the log in page.

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    PHP Code:
    <?PHP

        $db 
    = @mysql_connect("connection""root""password") or die("Could not establish connection with database");

        
    mysql_select_db('guestbook');

        
    $query "INSERT INTO Guests(UserName, Email, Comments, Date) Values('$UserName', '$Email', '$Comments', '".date("Y-m-d")."')";

        
    $result =  mysql_query($query) or die("Unable to enter your information.  Please try again later.");

        if 
    $result {
            
    header ("Location: [b]http://www.paulkjohnson.com/test.php[/b]");
        } else {
            
    header ("Location: [b]http://www.paulkjohnson.com/login.php[/b]");    
        }

    ?>
    I removed the <BODY> tags, because of what I said about no output before the header is called. Also, make sure you change the bold url's to the correct ones.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    That didn't work either. It probably needs more backslashes or forward slashes or whatever. I don't think I have the frustration tolerance to keep dicking with this. The language makes about no sense at all. So far, I've asked about how to use the slashes and all I've gotten are short blurbs that don't help me at all. The book I bought basically got me connected to the database and doesn't have any real world anything in it. This just sucks.

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    PHP isn't that hard dude. I picked it up in about a week.

    I don't see where you would need a backslash in that code. Just make sure that no html, no text, or anything is outputted before you call the header. And it should work.

    You could also try outputting a <meta> refresh and setting it to 0 so it's automatic.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    This is the complete file. There is no html output. All it does is put the data in the database and redirect.

    PHP Code:
    <HTML>

    <HEAD>

      <TITLEGuest book entry form-handler>

    </HEAD>

    <BODY>

      <?PHP

        $db 
    = @mysql_connect("connection""root""password") or die("Could not establish connection with database");

        
    mysql_select_db('guestbook');

        
    $query "INSERT INTO Guests(UserName, Comments, Date, Mail) Values('".addslashes(htmlspecialchars($UserName))."', '".addslashes(htmlspecialchars($Comments))."', '".date("Y-m-d")."', '$Mail')";

        
    $result =  mysql_query($query) or die("Unable to enter your information.  Please try again later.");

      
    ?>

    <SCRIPT LANGUAGE="javascript">
      document.location.href='guestbook.php'
    </script>

    </BODY>

    </HTML>

  11. #11

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    it just occurred to me that I didn't take out the "or die" part. I probably need to do that.

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    There is too html output. It starts with the <HTML> tag...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    ok, well, I don't know **** about html either. I thought those just told the browser what kind of page it is. So I just remove those tags?

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You need to remove all HTML before the header() redirect.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    OK, this is what I've got now, but it doesn't work either.

    PHP Code:

      <?PHP

        $db 
    = @mysql_connect("connection""root""password") or die("Could not establish connection with database");

        
    mysql_select_db('guestbook');

        
    $query "INSERT INTO Guests(UserName, Comments, Date, Mail) Values('".addslashes(htmlspecialchars($UserName))."', '".addslashes(htmlspecialchars($Comments))."', '".date("Y-m-d")."', '$Mail')";

        
    $result =  mysql_query($query// or die("Unable to enter your information.  Please try again later.");
        
    if $result {
            
    header ("Location: [url]http://www.paulkjohnson.com/guestbook.php[/url]");
        } else {
            
    header ("Location: [url]http://www.paulkjohnson.com/login.php[/url]");    
        }

      
    ?>

  16. #16
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    are you getting any errors?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #17

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    No. But I never do. But it's not inserting the data into the database and it redirects to a blank page. The java script I had before to redirect (after the php code) worked, but it would only redirect to the guestbook page whether there was an error or not.

  18. #18
    Addicted Member Martin Wilson's Avatar
    Join Date
    Mar 2002
    Location
    :)
    Posts
    236
    You are not supposed to have the <a> tag in the header() function...use:
    header ("Location: http://www.paulkjohnson.com/guestbook.php");
    What is the answer to this question?

  19. #19

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Martin, I think the forum added that a tag as well as the "target =". I don't have it in my code.

    My code is exactly what you showed, but it doesn't work. Whether I put in valid entries or not, it doesn't go into the database and it redirects to a blank page.

  20. #20

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I have to put the code inside quote brackets instead of php brackets because the forum adds extra stuff into the code.

    This doesn't work:
    <?PHP

    $db = @mysql_connect("connection", "root", "password") or die("Could not establish connection with database");

    mysql_select_db('guestbook');

    $query = "INSERT INTO Guests(UserName, Comments, Date, Mail) Values('".addslashes(htmlspecialchars($UserName))."', '".addslashes(htmlspecialchars($Comments))."', '".date("Y-m-d")."', '".addslashes(htmlspecialchars($Mail))."')";

    $result = mysql_query($query) // or die("Unable to enter your information. Please try again later.");

    if $result
    {
    header ("Location: http://www.paulkjohnson.com/public_html/guestbook.php");
    }
    else
    {
    header ("Location: http://www.paulkjohnson.com/public_html/login.php");
    }

    ?>
    But this does. It just doesn't give me the choice to send the user back if they enter invalid data.
    <?PHP

    $db = @mysql_connect("connection", "root", "password") or die("Could not establish connection with database");

    mysql_select_db('guestbook');

    $query = "INSERT INTO Guests(UserName, Comments, Date, Mail) Values('".addslashes(htmlspecialchars($UserName))."', '".addslashes(htmlspecialchars($Comments))."', '".date("Y-m-d")."', '".addslashes(htmlspecialchars($Mail))."')";

    $result = mysql_query($query) // or die("Unable to enter your information. Please try again later.");

    ?>

    <SCRIPT LANGUAGE="javascript">
    document.location.href='guestbook.php'
    </script>

  21. #21
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by cafeenman
    is it supposed to be like this maybe?
    PHP Code:
    """""""""""""'''''...............header ("Locationhttp:\/\/w\w\w\.p\a\u\l\k\j\o\h\n\s\o\n\.\c\o\m\/\p\u\b\l\i\c\_\h\t\m\l\/\g\u\e\s\t\b\o\o\k\.\p\h\p\"\)\;...'''''"""""" 
    I just added a lot of indisciminate dots, backslashes, single quotes and double quotes since there's no rhyme or reason as to when to use them.

    uh no...

    Just for S and G's, try this and see if you get an error:

    PHP Code:
    <?PHP 

    $db 
    = @mysql_connect("connection""root""password") or die("Could not establish connection with database"); 

    mysql_select_db('guestbook') or die(mysql_error()); 

    $query "INSERT INTO Guests(UserName, Comments, Date, Mail) Values('".addslashes(htmlspecialchars($UserName))."', '".addslashes(htmlspecialchars($Comments))."', '".date("Y-m-d")."', '".addslashes(htmlspecialchars($Mail))."')"

    $result mysql_query($query) or die(mysql_error()); 

    if 
    $result 

    header ("Location: http://www.paulkjohnson.com/public_html/guestbook.php"); 

    else 

    header ("Location: http://www.paulkjohnson.com/public_html/login.php"); 


    ?>
    Make sure that is all that is in your code. If that still doesn't work, make a new blank .php file, and put it in this only:

    PHP Code:
    <?php
      header
    ("Location: http://www.paulkjohsnon.com/");
    ?>
    If that doesn't work, then something's wrong with PHP itself.

    Also, if you don't want the url's parsed, unclick the first check box when you make the post "Automatically parse URLs"
    My evil laugh has a squeak in it.

    kristopherwilson.com

  22. #22

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Hobo, the first example you said to try for "S & G's" I've already done. I did it with the public_html and without. It really shouldn't be there.

    Also, I don't know what you're talking about with the check box for automatically parse url's. I've never seen any php options. That's all in the hands of my web host isn't it?

    I'm going to try to upload a test.php file with just the header code to see if that works. I'll post results here.

  23. #23

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    OK, I see the change you made in the result line (or die). The header snippet worked all by itself. I had it redirect to a different page though so I'd be sure it wasn't putting me back to the index.htm file. So that part is ok. I just changed the guest book handler to reflect your changes.

    It didn't work and redirected me to a blank page with no error being raised.

  24. #24
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by cafeenman
    you're talking about with the check box for automatically parse url's. I've never seen any php options. That's all in the hands of my web host isn't it?
    No...when you make a new post on VBForums, there's an option right under the textarea that says "Automatically Parse URLs."

    Unclick that whenever you want to post a URL and not have it turned into a link. Just a FYI.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  25. #25

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Thanks for trying to help. At this point, I'm ready to call it quits with PHP. I've never had this many problems with any language I've used. I've spent two solid days trying to get two lines of code to work and it still doesn't.

    Plus, the things that I do have working, I don't know why they work. So I've wasted an entire week, learned nothing and basically think PHP sucks.

    I have a really basic guest book that is about 25 lines of code total, that I had to copy just make work because there is no rhymoe or reason that I can find for when to use dots, double-quotes, single quotes and backslashes. The documentation on php.net has been worthless as has this $35 book. In fact, the only thing that does work is the stripslashes(htmlspecialchars(etc)) that I can't find documentation for anywhere. What I mean is I can't find documentation for the '". crap.

    So I think what I'm going to do is change my host to one that supports ASP since I already know VB. I probably could have done in an afternoon what I've spent a week not getting accomplished.

    OK... I'm done venting now. PHP just sucks.

  26. #26
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    What do you need to know?

    You can pretty much use " or ' whenever you want.

    If you are going to put a variable in there, then use " :

    $var = "x = $x";

    For the dots, it's just like & in vb.

    echo "This is " . " a dot echo!";

    I don't know what's confusing you, though.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  27. #27

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    What's confusing me is that I don't understand about 90% of what I've done so far.

    I don't know why this works:

    '".addslashes(htmlspecialchars($UserName))."'

    what's with the double quotes, single quotes and dots on both ends?

    It doesn't help that I don't know how to write html either. I've been using FrontPage which I can't use with my PHP pages unless I develop two pages - a master and one with PHP. Then whenever I want to make a change to the page, I have to change the master and paste the PHP code into it.

    After all that, I have to upload it all to see if it works. If it doesn't, then I have to go through all that again. This process is incredibly tedious. I'm still trying to get Apache, PHP and mySQL to work on my machine. I've got some of it working, but the whole thing is just really a pain in the butt.

    Plus... you've tried to get that code to work as have a few others. I don't understand it, but some of you guys do. If no one can get it working, then I'm not going to be able to accomplish what I really want (which isn't a guest book). The whole guest book idea was just to do a really simple project that included an online database that a user could add to and then see the results.

    What's going to happen when I want to do something more complex, like e-commerce? I can't help but think that will be a nightmare from start to finish. I can't even get a simple conditional redirection to work.

  28. #28
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    That's just the way SQL statements are setup. That requires single quotes around the values, so we put it in double quotes:

    PHP Code:
    "(id,name,age) VALUES ('1', 'Kris', '18')" 
    With your code, it's just running the function:

    PHP Code:
    "(id,name,age) VALUES ('" myfunc($var) . "', 'Kris', '18')" 
    The Functions:
    addslashes: http://www.php.net/manual/en/function.addslashes.php
    htmlspecialchars: http://www.php.net/manual/en/functio...ecialchars.php
    Last edited by The Hobo; May 12th, 2002 at 09:02 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  29. #29
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I know you've probably given up, but I'm still interested in what the problem was. Does this output anything?

    PHP Code:

      <?PHP

        $db 
    = @mysql_connect("connection""root""password") or die("Could not establish connection with database");

        
    mysql_select_db('guestbook');

        
    $query "INSERT INTO Guests(UserName, Comments, Date, Mail) Values('".addslashes(htmlspecialchars($UserName))."', '".addslashes(htmlspecialchars($Comments))."', '".date("Y-m-d")."', '$Mail')";

        
    $result =  mysql_query($query);
        if 
    $result {
            echo 
    "a";
        } else {
            echo 
    "b";
        }

      
    ?>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  30. #30

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    No. I tried that earlier and just tried it again. Does nothing. What I really don't get is that the database doesn't get the data added even if it's valid. There's something about that conditional statement that keeps the entire code from working.

    I've pulled my ASP book off the shelf. I think I may even understand it.

    The reason I started trying to learn PHP was because I didn't know what I was doing when I got my web hosting. I though ASP had to do with how I create my pages. I didn't realize that the host had to support it. So later down the road I found out my host doesn't support FrontPage extensions or ASP. They support PHP only.

    I think the best idea is to use the knowledge I already have (7+ years of VB) and go with ASP. PHP just isn't getting the job done. It amazes me that this entire site was built with PHP. It amazes me even further that nobody knows why my code isn't working. But I do appreciate everyone trying to help. PHP has just exceeded my frustration tolerance.

  31. #31
    scoutt
    Guest
    you guys are funny. what is this

    if $result


    should be like this

    if ($result)

    so try this.

    PHP Code:
    <?PHP

        $db 
    = @mysql_connect("connection""root""password") or die("Could not establish connection with database");

        
    mysql_select_db('guestbook');

        
    $query "INSERT INTO Guests(UserName, Email, Comments, Date) Values('$UserName', '$Email', '$Comments', '".date("Y-m-d")."')";

        
    $result =  mysql_query($query) or die("Unable to enter your information.  Please try again later.");

        if (
    $result) {
            
    header ("Location: http://www.paulkjohnson.com/test.php");
        } else {
            
    header ("Location: http://www.paulkjohnson.com/login.php");    
        }

    ?>
    so you are wondring about ' and "

    well here is the jist. if you are in php mode <? ?> between the brackets is php mode, then you must use ' or \" so it can parse the code between them. if you are not in php mode then you can use what ever. this ' or this "

    you cannot do this

    """""""""""""'''''...............header ("Location: http://www.paulkjoh\nso\n.com/public_h\tml/gues\tbook.php\");...'''''""""""

    as that would error out.

    the reason this works '".addslashes(htmlspecialchars($UserName))."' is because the . is added to it, so ". is escaping itself out of the statement. just don't worry about why it works for now. later down the road when you get use to php and undrstand it more then you will understand.

    I will look into it so i can explain it better.

  32. #32

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    That works scoutt. My question is why does

    if ($result)

    work, but this didn't work

    if ($result = 0)

    I swapped the conditional when I did this, but it didn't work at all.


    By the way.

    WOO HOO!

  33. #33
    scoutt
    Guest
    because $result = 0 won't do anything.

    what you need to do if you want to check a variable is to do it like this.

    if($result == 0)

    notice 2 = signs. that is the correct way to do it.

    check out the manual for Comparison Operators.

  34. #34
    scoutt
    Guest
    here read this little blurb about the ". stuff

    http://www.php.net/manual/en/languag...ors.string.php

    should give you an idea what is going on.

  35. #35
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Good job, scoutt.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  36. #36

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by scoutt
    because $result = 0 won't do anything.

    what you need to do if you want to check a variable is to do it like this.

    if($result == 0)

    notice 2 = signs. that is the correct way to do it.

    check out the manual for Comparison Operators.
    Actually, scoutt, I did use the two equal signs in my code, but it still didn't work. I just incorrectly posted what I did in the thread.

    My code was like this:
    PHP Code:
    if ($result == 0)
        {
          
    header ("Location: http://www.paulkjohnson.com/login.htm");
        }
    else
        {
          
    header ("Location: http://www.paulkjohnson.com/guestbook.php");
        } 

  37. #37
    scoutt
    Guest
    did you try

    if($result == '0'){

    with the quotes?

  38. #38

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    No, I didn't use single quotes. I didn't know I needed to.

  39. #39
    scoutt
    Guest
    you should use them for that purpose.jsut a good habit.

    also this

    if($result == '0'){

    is the same as this

    if(!$result){

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width