Results 1 to 10 of 10

Thread: Public variable like VB

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Public variable like VB

    Hi All,

    I have a problem. I like to know how to declare a variable publicly in php, as we do in VB - in module section in general, so that all the forms can handle it. I have four php files in a project. I like to use same variables that hold name, address etc. and use it in different pages. Is it possible ? Please help.

    Thank you so much in advance.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Public variable like VB

    If you mean constants, you could declare them in a file reserved for constants, and include that using require_once on any pages that need those constants.

    If you do mean variables, then no. Remember that PHP is a scripting language; variables don't persist beyond a single execution of the script, so what you're asking for doesn't make sense. Unless you mean that all of the files will be included into one other file at run-time, in which case you should declare the variables in the file that is the entry point for the script.

  3. #3
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Public variable like VB

    Maybe you can use something like database with an user column?
    Or cookies?
    Delete it. They just clutter threads anyway.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: Public variable like VB

    Thanks to both of you panagate and ThePigB for the response.

    Actually what I want is from the first page I am taking Name, Date of birth of the user. In respective three page I would like to show the user name and date of birth at the top. How can i make it ? Please tell me something. I appreciate a simple example, if possible.

    I am waiting for the reply.

    Thank you so much for the response once again.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: Public variable like VB

    Can you explain th wrong with the following lines of php code.

    File 1: Sess1.php

    PHP Code:
    <?php

    error_reporting
    (E_ALL);
    session_start();

    if(isset(
    $_POST["spreads"]) && ($_POST["major"]) && ($_POST["minor"]))
    {
          
    $_SESSION["spreads"]=$_POST["spreads"];
          
    $_SESSION["major"]=$_POST["major"];
          
    $_SESSION["minor"]=$_POST["minor"];
    }

    ?>
    HTML Code:
    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    <form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    TEST<br><br>
    <select name="spreads" size="8">
    <option value="Celtic Cross" selected>Celtic Cross</option>
    <option value="One Card">One Card</option>
    <option value="Two Cards">Two Cards</option>
    <option value="Three Cards">Three Cards</option>
    <option value="Eliphas Levy Wheel Spread(Guiley)">Eliphas Levy Wheel Spread(Guiley)</option>
    <option value="The Line Spread">The Line Spread</option>
    <option value="Seven Card Chakra Layout">Seven Card Chakra Layout</option>
    <option value="Four Seasons Spread(Cortese/Reed)">Four Seasons Spread(Cortese/Reed)</option>
    </select>
    <br><br>
    <input type="checkbox" name="major" value="Major">Major
    &nbsp;&nbsp;
    <input type="checkbox" name="minor" value="minor">Minor
    &nbsp;&nbsp;
    <br><br>
    <input type="button" name="b" value="Send" onclick="Send()">
    </form>
    </body>
    
    <script language="javascript">
    
    function Send()
    {
          location.href="sess2.php";
    }
    
    </script>
    [/html]

    File 2: Sess2.php

    PHP Code:
    <?php

    error_reporting
    (E_ALL);
    session_start();
    $spreads="";
    $major="";
    $minor="";

    if(!empty(
    $_SESSION["spreads"]))
    {
          
    $spreads=$_SESSION["spreads"];
          
    $major=$_SESSION["major"];
          
    $minor=$_SESSION["minor"];
    }

    echo 
    "Spreads: ".$spreads."<br>";
    echo 
    "Major: ".$major."<br>";
    echo 
    "Minor: ".$minor."<br>";

    session_destroy();

    ?>
    When I am viewing the result in the browser I cannot get the values.

    I am waiting for your kind reply. Please help.
    Last edited by systech44; Aug 31st, 2007 at 05:49 AM.

  6. #6
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Public variable like VB

    Well, lets start at the Sess1.php.
    What the php part does is retrieve post information, but as you don't post anything, this code is useless there.
    That part should belong in Sess2.php

    Next stop, HTML.
    I see some inconsistencies and problems
    Example:
    HTML Code:
    <form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    The problem with this, is that it posts to the same page, which would fire the first PHP script and which is fine, but the problem is that you don't post anything.
    I'd change it to Sess2.php, so that you don't have to double the submission.

    HTML Code:
    <input type="checkbox" name="major" value="Major">Major
    &nbsp;&nbsp;
    <input type="checkbox" name="minor" value="minor">Minor
    &nbsp;&nbsp;
    <br><br>
    <input type="button" name="b" value="Send" onclick="Send()">
    This part is what I meant with inconsistencies.
    It's not a big deal, but I'd close the tags. (you can do this in the same tag)

    HTML Code:
    <script language="javascript">
    
    function Send()
    {
          location.href="sess2.php";
    }
    
    </script>
    Javascripts, go in the header, and actually this part is completely unnecessary if you use a submit button.

    PHP Code:
    <?php

    error_reporting
    (E_ALL);
    session_start();
    $spreads="";
    $major="";
    $minor="";

    if(!empty(
    $_SESSION["spreads"]))
    {
          
    $spreads=$_SESSION["spreads"];
          
    $major=$_SESSION["major"];
          
    $minor=$_SESSION["minor"];
    }

    echo 
    "Spreads: ".$spreads."<br>";
    echo 
    "Major: ".$major."<br>";
    echo 
    "Minor: ".$minor."<br>";

    session_destroy();

    ?>
    PHP is not like VB. You don't have to declare variables before you use them.
    If you change the last page, all you have to do is use the $_POST array to retrieve the data from the last page.
    If you still want to use the session in other pages, you leave that code here and you might want to remove the session_destroy line.
    If you don't need the rest, the overall use of a session is completely unnecessary.
    And one other thing. If you use double quotes for a string, variables are taken in as the variables value. So no need to close it - use variable - and open it.



    So this should end up:

    Sess1.php
    HTML Code:
    <html> 
    <head> 
    	<title>Test</title> 
    </head> 
    <body> 
    	<form id="f" name="f" method="post" action="Sess2.php"> 
    		TEST<br><br> 
    		<select id="spreads" name="spreads" size="8"> 
    			<option value="Celtic Cross" selected>Celtic Cross</option>
    			<option value="One Card">One Card</option>
    			<option value="Two Cards">Two Cards</option>
    			<option value="Three Cards">Three Cards</option>
    			<option value="Eliphas Levy Wheel Spread(Guiley)">Eliphas Levy Wheel Spread(Guiley)</option>
    			<option value="The Line Spread">The Line Spread</option>
    			<option value="Seven Card Chakra Layout">Seven Card Chakra Layout</option>
    			<option value="Four Seasons Spread(Cortese/Reed)">Four Seasons Spread(Cortese/Reed)</option>
    		</select> 
    		<br><br>
    		<input type="checkbox" id="major" name="major" />Major &nbsp;&nbsp; 
    		<input type="checkbox" id="minor" name="minor" />Minor &nbsp;&nbsp; 
    		<br><br> 
    		<input type="submit" id="b" name="b" value="Send" /> 
    	</form> 
    </body>
    </html>
    Sess2.php
    PHP Code:
    <?php

    session_start
    ();

    if(isset(
    $_POST['b']))
    {
        
    $spreads $_POST['spreads'];
        
    $major $_POST['major'];
        if(
    $major == "on"){
            
    $major "Checked";
        }
        
    $minor $_POST['minor'];
        if(
    $minor == "on"){
            
    $minor "Checked";
        }
        
    $_SESSION['spreads'] = $spreads;
        
    $_SESSION['major'] = $major;
        
    $_SESSION['minor'] = $minor;
    }else{
        echo 
    "Invalid input";
    }

    echo 
    "Spreads: $spreads<br>";
    echo 
    "Major: $major<br>";
    echo 
    "Minor: $minor<br>";

    ?>
    Last edited by TheBigB; Sep 1st, 2007 at 12:23 PM. Reason: Tested it. Should work now.
    Delete it. They just clutter threads anyway.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: Public variable like VB

    Hi TheBigB AND ALL. A bit long time later. I like to say something.

    I use your style of coding in my latest files. But due to some unknown reason from my side I found the following errors.

    Notice: Undefined index: C75 in /home/go/public_html/palmistry/result.php on line 9

    Notice: Undefined index: C76 in /home/go/public_html/palmistry/result.php on line 10

    I am adding the code. Please take a look. I need help.

    File 1:
    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    ?>
    HTML Code:
    <html>
    <head>
    <title>MB Free Palmistry</title>
    </head>
    <body>
    <form id="girdleofvenus" method="POST" name="girdleofvenus" action="result.php">
    	<table border="1" width="100%" id="table1">
    		<tr>
    			<td width="280">Girdle Of Venus</td>
    			<td>&nbsp;</td>
    		</tr>
    		<tr>
    			<td width="280">Significance</td>
    			<td>The Girdle of Venus is associated with emotions, intelligence and temperament.</td>
    		</tr>
    		<tr>
    			<td width="280">Location</td>
    			<td>The Girdle of Venus is a semicircle that rises between first and
    			second fingers and finishes between third and fourth.</td>
    		</tr>
    		<tr>
    			<td width="280">&nbsp;</td>
    			<td>Compare your hand with the images displayed. Choose all the
    			features that stand true for your hand.</td>
    		</tr>
    		<tr>
    			<td width="280">
    			<input border="0" src="pics/girdleofvenus/gridle_1.jpg" name="I1" width="137" height="169" type="image"></td>
    			<td><input type="checkbox" id="C75" name="C75" /> My Girdle of Venus
    			is unbroken</td>
    		</tr>
    		<tr>
    			<td width="280">
    			<input border="0" src="pics/girdleofvenus/gridle_2.jpg" name="I2" width="137" height="169" type="image"></td>
    			<td><input type="checkbox" id="C76" name="C76" /> My Girdle of Venus
    			is fragmented</td>
    		</tr>
    		<tr>
    			<td width="280">&nbsp;</td>
    			<td><input type="checkbox" id="C77" name="C77" /> I cannot see the
    			Girdle of Venus</td>
    		</tr>
    	</table>
    	<p align="center"><input type="submit" id="b" name="b" value="View Result" /></p>
    </form>
    </body>
    <script language="javascript">
    </script>
    </html>
    File 2:
    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    session_start();

    if(isset(
    $_POST["b"]))
    {
        
    $a=$_POST["C75"];
        
    $b=$_POST["C76"];
        
    $c=$_POST["C77"];
        if(
    $a=="ON")
            
    $a="on";
        else
            
    $a="off";

        if(
    $b=="ON")
            
    $b="on";
        else
            
    $b="off";

        if(
    $c=="ON")
            
    $c="on";
        else
            
    $c="off";

    $_SESSION["a12_01"]=$a;
    $_SESSION["a12_02"]=$b;
    $_SESSION["a12_03"]=$c;
    }

    ?>
    Please help.

    Thank you in advance.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Public variable like VB

    PHP is not like VB. You don't have to declare variables before you use them.
    You don't have to but you should. to declare a variable simply inistialize it:
    $s = '';

    If you leave notices on you will see when you have used a variable before declaring it you get a notice spat out. If you use this feature properly the notice will indicate an error.

    Because HTTP (the protocol that you use to communicate with the web server) is stateless, data betweeen requests will be lost. PHP does have global variables but these variables are global to the script and any included script - that is exactly the same as VB.

    Now think of a VB program. You want to save data and use it the next time you run the program. Data such as the users preferences. In order to do this you save that information to a file or database or the registry. PHP is the same, expect the "program" runs in its entirity with each request and any data you wish to persist between these requests must be saved.

    PHP's session module will do this all for you. You simply call session_start(); before you use the $_SESSION array and loard and retrive data from it like you would the $_POST array.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: Public variable like VB

    Thank you VisualAd. Can you please tell me why I am getting these notices ? This is important for me. Please help. Thanks.

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Public variable like VB

    Because you have no initialized the variable before you use it:
    PHP Code:
    echo $string// this will produced a notice because it was never assigned a value.

    $int 1// variable is initialized.

    echo $int// this will not produce a notice because the variable been initialized 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

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