Results 1 to 14 of 14

Thread: function.. [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    philippines
    Posts
    245

    Resolved function.. [RESOLVED]

    i am trying to create a function.. but i don't know how to use it.. a procedure(as in VB) but how?

    I don't want it to be in a different file...

    PHP Code:
    <html>
    <head>sample</head>
    <body>
           *********** how will i use dbconnect in here **********
    </body>
    </html>
    <?php function dbconnect($host,$user,$password)
    $conn mysql_connect($host$user$password
    or die (
    "Could Not Connect to DB"); 
    ?>
    a sample of function and procedure and how to use it will be excellent.. sorry just started PHP yesterday.. hope you guys can help me..
    Last edited by titan7262; Sep 12th, 2005 at 02:16 AM.

  2. #2
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: function..

    My experience is limited with PHP but shouldn't it be like this:

    PHP Code:
    Code:
    <html> 
    <head>sample
    <?php function dbconnect($host,$user,$password) ;
    $conn = mysql_connect($host, $user, $password) ;
    or die ("Could Not Connect to DB"); 
    ?> </head> 
    
    <body> 
           *********** how will i use dbconnect in here ********** 
    <?php 
    dbconnect($host,$user,$password);
    ?>
    </body> 
    </html>
    PHP/HTML reads a file from top to bottom I suspect so function definitions should be made before they are used. and I'm not sure about the orange semicolon
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    philippines
    Posts
    245

    Angry Re: function..

    ok... i have solved most of my problems now.. umm how can i add items to a list on my form using the records from my database? <- got this working.. but the problem is.. the results are shown on a diffrent page.. i am wondering how can i expose all results on the same page.. i will post the code..

    PHP Code:
    <html>
    <head>
    </body>
    <form>
      <p> 
        <select name="mGrid" id="mGrid">
          <option value="NONE">----- SELECT GRID -----</option>
          <option value="NORTH">NORTH</option>
          <option value="EAST">EAST</option>
          <option value="SOUTH">SOUTH</option>
          <option value="WEST">WEST</option>
        </select>
        <input type="button" name="Button" value="Button" onClick="show()"> ** how can i call show after clicking the button?? **
        <br>
        <br>
        <select name="select" size="10">
          <option>--------------- LIST OF BRANCHES ---------------</option>
        </select>
    </form>
    </html>
    <? function show(){
    $host = 'localhost'; 
    $user = 'root'; 
    $password = 'admin'; 
    $conn = mysql_connect($host, $user, $password) 
    or die ("Could Not Connect to DB"); 

    $mooo = '$mGrid';
    $sd = mysql_select_db("ifl-sales", $conn) 
    or die ("Could Not Select DB" . mysql_error()); 
    $tableaccess = "select branch_name from branch where grid = '$mooo'";
    $sb = mysql_query($tableaccess, $conn); 
    while ($row = mysql_fetch_array ($sb)) 
        { 
        echo("<option>");
        echo($row['branch_name']);
        }
    }
    ?>

    everytime ic click the button nothing happens..
    Last edited by titan7262; Sep 8th, 2005 at 04:45 AM.

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: function..

    You are trying to call the PHP function from the client side, this wont work. All the PHP gets processed before it gets to the client. So they will never see your PHP code. Try using a form eg

    PHP Code:
    <form method="POST">
    <input type="submit" name="submit1" value="Get Values"/>
    <input type="hidden" name="action" value="getvalues" />
    </form>
    <?
    if($_GET['action']=="getvalues")
    {
         show(); // <--- calling your function here;
    }
    ?>
    That will basicaly do what you want, but the page will be reloaded, which really cant be helped.

    There is a way to get the data as you click the button, but that uses javascript as well and is a little complex, ill just find the link for you.

  5. #5

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    philippines
    Posts
    245

    Re: function..

    <form method="POST">
    <input type="submit" name="submit1" value="Get Values"/>
    <input type="hidden" name="action" value="getvalues" />
    </form>
    <?
    if($_GET['action']=="getvalues")
    {
    show(); // <--- calling your function here;
    }
    ?>
    for what is the <input type="hidden" name="action" value="getvalues" /> for? and how will my page be loaded i can't seem to find how it is reloaded..


    so i need to learn javascript for me to be able to call php functions?

  7. #7
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: function..

    Use use a form to submit the data to the php script. The <input type="hidden" name="action" value="getvalues" /> is so that the part of your script you want to run only happens after you press the button.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    philippines
    Posts
    245

    Re: function..

    sorry.. but i can't get it to work..

    <html>
    <head>
    </body>
    <?php include("show.php")?>
    <form method="post">
    <p>
    <select name="mGrid" id="mGrid">
    <option value="NONE">----- SELECT GRID -----</option>
    <option value="NORTH">NORTH</option>
    <option value="EAST">EAST</option>
    <option value="SOUTH">SOUTH</option>
    <option value="WEST">WEST</option>
    </select>
    <input type="button" name="Button" value="Button">
    <input type="hidden" name="action" value="Button">
    <br>
    <br>
    <select name="select" size="10">
    <option>--------------- LIST OF BRANCHES ---------------</option>
    </select>
    </form>
    <?
    if($_GET['action']=="button")
    {
    show();
    }
    ?>
    </html>

    what's wrong with this.. it says error on line 23 which is if($_GET['action']=="button") <-- that's the line

  9. #9
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: function..

    You need to change two things, one PHP is case sensative, and secondly you are not using a submit button on your form, you are using a normal button try
    Code:
     <html>
    <head>
    </body>
    <?php include("show.php")?>
    <form method="post">
    <p>
    <select name="mGrid" id="mGrid">
    <option value="NONE">----- SELECT GRID -----</option>
    <option value="NORTH">NORTH</option>
    <option value="EAST">EAST</option>
    <option value="SOUTH">SOUTH</option>
    <option value="WEST">WEST</option>
    </select>
    <input type="submit" name="Button" value="Button">
    <input type="hidden" name="action" value="button">
    <br>
    <br>
    <select name="select" size="10">
    <option>--------------- LIST OF BRANCHES ---------------</option>
    </select>
    </form>
    <?
    if($_GET['action']=="button")
    {
    show();
    }
    ?>
    </html>
    That should work now

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    philippines
    Posts
    245

    Re: function..

    i am getting this error..

    Notice: Undefined index: action in D:\PHPWEB\HTMLKIT\temp.php on line 24

    line 24 is: if($_GET['action']=="button")

  11. #11
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: function..

    ahhh im half asleep try using
    PHP Code:
    <?
    if(isset($_POST['action']))
    {
        if($_POST['action']=="button")
        {
            show();
        }
    }
    ?>

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    philippines
    Posts
    245

    Re: function..

    ok that did the trick.. and a new error has occured.. sorry if i am bugging you this much..

    this is the whole code..

    PHP Code:
    <html>
    <head>
    </body>
    <?php include("show.php")?>
    <form method="post">
    <p>
    <select name="mGrid" id="mGrid">
    <option value="NONE">----- SELECT GRID -----</option>
    <option value="NORTH">NORTH</option>
    <option value="EAST">EAST</option>
    <option value="SOUTH">SOUTH</option>
    <option value="WEST">WEST</option>
    </select>
    <input type="submit" name="Button" value="Button">
    <input type="hidden" name="action" value="button">
    <br>
    <br>
    <select name="select" size="10">
    <option>--------------- LIST OF BRANCHES ---------------</option>
    </select>
    </form>
    <? 
    if(isset($_POST['action'])) 

        if($_POST['action']=="button") 
        { 
            mshow()
        } 

    ?> 
    </html>
    <? 
    function mshow(){
    $host = 'localhost'; 
    $user = 'root'; 
    $password = 'admin'; 
    $conn = mysql_connect($host, $user, $password) 
    or die ("Could Not Connect to DB"); 

    $mooo = '$mGrid';
    $sd = mysql_select_db("ifl-sales", $conn) 
    or die ("Could Not Select DB" . mysql_error()); 
    $tableaccess = "select branch_name from branch where grid = '$mooo'";
    $sb = mysql_query($tableaccess, $conn); 
    while ($row = mysql_fetch_array ($sb)) 
        { 
        echo("<option>");
        echo($row['branch_name']);
        }
    }
    ?>
    nothing happens.. it just redraw the page but did not show the results..
    Last edited by titan7262; Sep 8th, 2005 at 10:11 PM.

  13. #13
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: function..

    You don't have superglobals turned on so change
    PHP Code:
    $mooo '$mGrid'
    to

    PHP Code:
    $mooo $_POST['mGrid']; 
    It *should* work now.....hopefuly.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    philippines
    Posts
    245

    Re: function..

    just found some samples on other websites.. Thanks to all that helped!!!

    CHEERS!

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