Testing a PHP script on a local computer

Discussion in 'PHP' started by jackson7, Jan 16, 2009.

  1. #1
    New to PHP and web development in general. I am trying to get information from an HTML form to appear in a table on another web page after clicking submit. So I installed Apache and then PHP on my local PC and expected to be able to test a PHP script locally however it does not return the information I was expecting. The following is the code for the form:
    
    <form method="post" action="showform.php">
    
    Please fill out the following form if you would like to be contacted: <br/>
            Name:<input type="text" name="name" /> <br/><br/>
            Company: <input type="text" name="company"/> <br/><br/>
            Phone: <input type="text" name="phone" /> <br/><br/>
            Email: <input type="text" name="email" /> <br/><br/>
    
            <input type="submit" name="Submit" value="Submit" />
    </form>
    
    The following is the code for the php script:
    
    <table>
    <tr><th>Field Name</th><th>Value(s)</th></tr>
    
    [php]
    <?php
    if (empty($_POST)) {
    print "<p>No data was submitted.</p>";
     } else {
    
    foreach ($_POST as $key => $value) {
    if (get_magic_quotes_gpc()) $value=stripslashes($value);
    if ($key=='extras') {
    
    if (is_array($_POST['extras']) ){
            print "<tr><td><code>$key</code></td><td>";
            foreach ($_POST['extras'] as $value) {
                            print "<i>$value</i><br />";
                            }
                            print "</td></tr>";
            } else {
            print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
            }
    } else {
    
    print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
    }
     }
    }
    ?>
    </table>
    </body>
    </html>
    
    I know it works when used on the internet but how come it doesn't work locally. I have checked that apache and php are installed correctly. What could be the issue? The current result is a table with $key and $value in the places where the correct values should be, in other words in the table cells. I have also tried using XAMPP as well. Thanks for your help.
    Code (markup):

     
    jackson7, Jan 16, 2009 IP
  2. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #2
    If you installed apache or php wrong, it could be causing problems.

    Try setting up a localhost. http://www.wampserver.com

    It is very helpful, and runs exactly like a web server would.
     
    crath, Jan 16, 2009 IP
  3. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    my .02 cents.

    i used to develop on my local and move working code to the server.
    i finally got tired of spending hours making something work on my local, only to find out the local server / hosting server are different OR different version of php.

    so, i NEVER develop local anymore :)
    its not worth my time do REDO my code for the hosting server
     
    cl328, Jan 16, 2009 IP
  4. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #4
    That is why you must keep your software up to date ;)

    I've never had any problems moving from localhost to online.

    If you set things up properly, All you will need todo is fix the sql connection settings when you move it, and make sure you move your db's and all, and you should be set.
     
    crath, Jan 16, 2009 IP
  5. jackson7

    jackson7 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Wampserver worked Crath thank you for your help! It is much appreciated.
     
    jackson7, Jan 16, 2009 IP
  6. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #6
    No problem.

    Also, you are using a very weird way to print your form results on the next page.

    You can just use something like echo $_POST['field_name']

    unless you have some other reasons to be doing it that way, if so, don't mind me :p

    enjoy :)
     
    crath, Jan 16, 2009 IP
  7. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    my point --- you can't always trust your hosting server to be identical to your local
    :)
     
    cl328, Jan 16, 2009 IP
  8. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #8
    Do you use functions that are that new? Shouldn't be too different.
     
    crath, Jan 16, 2009 IP
  9. cl328

    cl328 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    its been a few years ago, that i stopped developing local.

    ps. i know from chaning hosting companies.... 2 servers that were supposed to be the same, well, there are differences.... in settings, in paths, in minor things ..... the more advanced coding you have... the more these small details come into play
     
    cl328, Jan 16, 2009 IP
  10. Jeremy Morgan

    Jeremy Morgan Peon

    Messages:
    322
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Personally, I hand roll everything (install php, apache and mysql by hand and tie them together so I can customize everything) but for beginners that can be a nightmare. I recommend using Zend Core:

    http://www.zend.com/products/core/

    It installs everything for you, ties it all together and gives you a little management console.

    Best of all, it's 100% free. I would recommend it for any new developers wanting to run php scripts. Combine this with Notepad++ and you have everything you need to start php development, for free!
     
    Jeremy Morgan, Jan 16, 2009 IP
  11. fabregas2014

    fabregas2014 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    u can use XAMP or WAMP server it contains Apache + php + mysql all u need to run ur site on ur local pc..
     
    fabregas2014, Jan 16, 2009 IP
  12. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #12
    No offence intended but your code must be pretty poor if you need to change that much. Assuming your server has at least PHP 5, preferably 5.2+ (and if they didn't, why would you host with someone who can't keep their free software up to date?) then there's next to nothing that would need alterating between local and server.

    I personally write everything locally, all I have to change is the database details and a few lines in a config file and everything works perfectly.

    Dan.
     
    Danltn, Jan 16, 2009 IP
    qualityfirst likes this.
  13. akel

    akel Guest

    Messages:
    1,024
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    0
    #13
    php not installled properly....i think it just came as text dowciments....download wampserver or xamp,they will install php,mysql...etc in a single click
     
    akel, Jan 16, 2009 IP
  14. Jeremy Morgan

    Jeremy Morgan Peon

    Messages:
    322
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Jeremy Morgan, Jan 17, 2009 IP
  15. Speakup

    Speakup Banned

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Just use wamp on your computer installs MYSQL and PHP for you.
     
    Speakup, Jan 17, 2009 IP