How to use $_SERVER['PHP_SELF'] properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paitoon
    New Member
    • Aug 2007
    • 62

    How to use $_SERVER['PHP_SELF'] properly

    Hello there,
    The fuction $_SERVER['PHP_SELF']; is nice to use but it so complicate for me...
    In my site to try to use this to add the information to database..but it work not correct because it will work every time i open that page or even i click to reflesh window......eve n i use funtion if or even i use window confirm msg....they can not help.
    please help me to explain what should i do with this to stop the script to not work before i sut mit it.
    thank you so much
    Paitoon
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Paitoon.

    What do you want your code to do? Give an example.
    What is your code doing that you don't want it to do? Give an example.
    What is your code *not* doing that it is supposed to? Give an example.

    Comment

    • paitoon
      New Member
      • Aug 2007
      • 62

      #3
      What do you want your code to do? Give an example.
      What is your code doing that you don't want it to do? Give an example.
      What is your code *not* doing that it is supposed to? Give an example.



      I want $_SERVER['PHP_SELF']; work proerly, I want it work only when people click to submit it....Because now it work every time when people visit that site or even click reflesh window....
      because $_SERVER['PHP_SELF']; has the form and the working script in the same page right?
      so because i dont know how to use it properly.
      Paitoon

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I want it work only when people click to submit it.
        Click what to submit what?
        You need to have a test of some sort within the script to make this decision

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Originally posted by paitoon
          I want $_SERVER['PHP_SELF']; work proerly, I want it work only when people click to submit it....Because now it work every time when people visit that site or even click reflesh window....
          because $_SERVER['PHP_SELF']; has the form and the working script in the same page right?
          so because i dont know how to use it properly.
          Paitoon

          So Its a form, right. Is it submitting automatically without user interaction? If so check it out the body onload values.

          Comment

          • paitoon
            New Member
            • Aug 2007
            • 62

            #6
            How to work with <?php echo $_SERVER['PHP_SELF'] ?> properly

            I have problem to work with [code=php]<?php echo $_SERVER['PHP_SELF'] ?> [/code]
            For eksampel work with function delete or add friend.....
            I want to know how to make it work properly...Beca use it always work at once without click any button.

            For example i will make add friend list.......the form to add friend is the same page with working script to add info to database ...
            then everytime i open that page or reflesh it the friend will be added at once without clicking any button....


            what should i do ?
            thank you
            Alex
            Last edited by pbmods; Sep 15 '07, 07:05 PM. Reason: Added [CODE] tags.

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Moving thread to the forum......

              DO NOT POST QUESTIONS HERE This thread is located at the top of Php articles section. please read it carefully before you post a question in the article section.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                To fully understand your problem, show your coe (within code tags) in this forum.

                ROnald

                Comment

                • paitoon
                  New Member
                  • Aug 2007
                  • 62

                  #9
                  [code=php]
                  <?

                  include "connect.ph p";
                  $sql="select * from users order by username";
                  $result=mysql_d b_query($dbname ,$sql);
                  $num=mysql_num_ rows($result);

                  while($r=mysql_ fetch_array($re sult)) {
                  $username=$r[username];
                  $password=$r[password];


                  $serv=@ $_SERVER['PHP_SELF'];

                  echo" <td ><div id='searchDivLi nk'>
                  <a href ='$serv? adder=$valueadd er&friend=$valu efriend' onclick='return confirm('Are you sure ?');>add friend</a>";


                  }

                  $adder=$_GET['adder'];
                  $friend=$_GET['friend'];
                  $dt = date("Y-m-d");
                  if ($confirm){

                  $sql7="insert into friend_list values('','$add er','$friend',' $dt')";
                  $result7= mysql_db_query( $dbname,$sql7);

                  }


                  ?>
                  [/code]
                  Last edited by Atli; Sep 16 '07, 05:07 PM. Reason: Added [code] tags

                  Comment

                  • kovik
                    Recognized Expert Top Contributor
                    • Jun 2007
                    • 1044

                    #10
                    Originally posted by paitoon
                    [php]$serv=@ $_SERVER['PHP_SELF'];

                    echo" <td ><div id='searchDivLi nk'>
                    <a href ='$serv? adder=$valueadd er&friend=$valu efriend' onclick='return confirm('Are you sure ?');>add friend</a>";[/php]
                    Firstly, you don't need to use $serv at all. You are using a relative link anyway, so you can completely omit the link part and leave only the query string, and browser will automatically interpret it as a link to the same page plus that query string.

                    Originally posted by paitoon
                    [php]if ($confirm){[/php]
                    Secondly, where in the world does $confirm come from?

                    Comment

                    • paitoon
                      New Member
                      • Aug 2007
                      • 62

                      #11
                      hehe actually i just put it...because i put the popup confirm window......
                      i dont know what should i do....

                      Comment

                      • aktar
                        New Member
                        • Jul 2006
                        • 105

                        #12
                        See annotation of your code below


                        [PHP]

                        <? //if you're running php5 maybe you should use <?php instead


                        include "connect.ph p";
                        $sql = "select * from users order by username";
                        $result = mysql_db_query( $dbname,$sql);
                        $num = mysql_num_rows( $result);

                        while($r=mysql_ fetch_array($re sult))
                        {
                        //Your problem may lies in the two lines below, as you are trying to pass literal aggregates as constants
                        //$username = $r[username];
                        //$password = $r[password];

                        //It should be like this :
                        $username = $r["username"];
                        $password = $r["password"];

                        //this is unnecessary, see anchor below
                        //$serv = @$_SERVER['PHP_SELF'];

                        echo " <td >
                        <div id='searchDivLi nk'>";

                        //dont do it like this
                        //<a href ='$serv? adder=$valueadd er&friend=$valu efriend' onclick='return confirm('Are you sure ?');>add friend</a>";


                        //do it like this : [notice the page name missing?? you dont need it since its going back to the same page]
                        echo " <a href ='?adder=$value adder&friend=$v aluefriend' onclick='return confirm('Are you sure ?');>add friend</a>";

                        }

                        $adder = $_GET['adder'];
                        $friend = $_GET['friend'];
                        $dt = date("Y-m-d");

                        // if ($confirm)
                        // {

                        //this $confirm variable : are you sure you're actually receiving a
                        //boolean TRUE. To be sure run a little debug
                        //if not then this is the whole root of your problems
                        //I think the javascript confirms that the requested page should be
                        //loaded. try the following

                        if (!empty($adder) && !empty(friend))
                        {
                        $sql7 = "insert into friend_list values('','$add er','$friend',' $dt')";
                        $result7 = mysql_db_query( $dbname,$sql7);
                        }
                        ?>

                        [/PHP]


                        Hope it helps

                        Comment

                        • paitoon
                          New Member
                          • Aug 2007
                          • 62

                          #13
                          it still work wrong..

                          even i only refresh the page the new friend was added at once.....
                          or even i just open this page without click any button....the friend was added also...


                          i got so stress
                          Paitoon

                          Comment

                          • paitoon
                            New Member
                            • Aug 2007
                            • 62

                            #14
                            can somebody here tell me why ?
                            and what should i do ?
                            thank a lot
                            Paitoon

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              Heya, Paitoon.

                              What's your code look like now, and what is it doing that it should not be doing?

                              Comment

                              Working...