Web Analytics Made Easy -
StatCounter Naming a variable from a form to be used in other pages - CodingForum

Announcement

Collapse
No announcement yet.

Naming a variable from a form to be used in other pages

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Naming a variable from a form to be used in other pages

    I am working on a text RPG project and want to incorporate a little server-side scripting. I have never used PHP before and tried to do some research on w3schools. I got only little of what I need. I have a home page that looks a little like this.

    Code:
     <div class="content">
                <p>Welcome to Miriam's Quest!</p>
                <p>To begin, insert your name below!</p>
                <form action="job.html" method="get">
                    <input type="text" name="name">
                    <input type="submit" value="Continue">
                </form>
            </div>
    The "action" attribute seems to be the most troubling.
    This is what job.html looks like for now.

    Code:
    <body>
            <?php
            $name = $_GET["name"]
            ?>
            <div class="header">
                <h1>Miriam's Quest</h1>
            </div>
            <div class="nav">
                <p><a href="home.html">Back to Start</a></p>
            </div>
            <div class="content">
                <p>Hello <?php
                echo "Hello $name ;
                ?>
                </p>
            </div>....
    The content should say Hello _____ (whatever the player filled out in the home page)
    Now obviously I cut parts out but I wanted to keep in the PHP. I toyed around with having a separate PHP file but it wasn't working either.
    If anyone could help me understand what I'm doing wrong it would be greatly appreciated. I iterate again that I have barely touched this script language but am looking to get better at it so a straight answer won't be as helpful as an explanation.

  • #2
    Do you have your webhost set-up to run .html extensions as PHP?
    Normally, your extension should be .php if you wish to run PHP scripting.

    Change this:

    <p>Hello <?php
    echo "Hello $name ;
    ?>

    To this:

    <p>Hello <?php echo $name; ?></p>

    You have quotes missing or not necessary. End your PHP commands with semicolon ;
    Last edited by mlseim; Sep 12, 2016, 06:56 PM.

    Comment


    • #3
      Ok thanks for the help! I bring a question then...
      If I still wanted to run my website on HTML and CSS, but incorporate PHP, how should I go about doing that? (If it's possible) I was using w3schools and it seems to me that maybe you can only run html with php, and not CSS. Would I be able to still reference outside stylesheets and javascript in a PHP file? Should I run all my webpages as a PHP file?
      Again I am very new to PHP and do not fully understand the incorporation of it. Thanks for all the help!

      Comment


      • #4
        Originally posted by olefalcon View Post
        Would I be able to still reference outside stylesheets and javascript in a PHP file?
        Certainly, would be pretty useless if you couldn't.

        While the file extension may be different, once the PHP runs on the server-side what it spits out to the browser is still HTML, even with the different file extension. All your href, src, etc, etc will behave just as they did if it were a static .html file. The browser doesn't care because it NEVER see's the PHP code. Nothing you put between <?php and ?> is sent to the browser unless you tell it to with a command like echo or print.
        Walk the dark path, sleep with angels, call the past for help.
        https://cutcodedown.com
        https://medium.com/@deathshadow

        Comment


        • #5
          Thank you for helping me grasp the concept! I am going to continue my project as php files and see how it goes!

          Comment


          • #6
            Technically, you can run .html or .htm as PHP scripts, by specifying within an .htaccess file. But for all general purposes, it's normal and acceptable to use .php

            If you're learning, using .php extensions is less confusing, especially if you use something like Notepad++ as your script editor. It's just easier.

            Comment

            Working...
            X