Thread: Some Javascript help

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Some Javascript help

    It's been some time I have messed with Javascript and PHP, and to say the least, I am rusty. So I was going to ask here to see if anyone with more experience can get this working.
    Basic goal: pass information from HTML page (via hyperlinks) to PHP script.
    I have tried several different approaches, but none of them seems to work properly.

    The current code is as follows:
    Code:
    <?php
    session_start();
    if ($_POST["region"] == "")
    {
        include_once "page_top.html";
        ?>
        <script language="javascript">
        function SubmitPal()
        {
            var a_span = document.createElement("span");
            var form = document.createElement("form");
            var region = document.createElement("input");
            form.setAttribute("method", "post");
            form.setAttribute("action", "<?php echo $_SERVER['PHP_SELF'] ?>");
            region.setAttribute("name", "region");
            region.setAttribute("value", "pal");
            region.setAttribute("type", "hidden");
            form.appendChild(region);
            a_span.appendChild(form);
            alert("Submitting...");
            //alert(form.innerHTML);
            alert(a_span.innerHTML);
            //form["region"].value = 'pal';
            form.submit();
        }
        </script>
        <!--<form action="<?php echo $_SERVER['PHP_SELF'] ?>" id="submit_" method="post">
        <input type="text" name="region" value="ntsc" />
        <input type="submit" value="submit" />
        </form>-->
        <?php
        if ($_SESSION["timeout"] == "Yes")
        {
            echo "
                <h2>Oops!</h2>
                Det verkar som din session har g&aring;tt ut. Var v&auml;nlig svara p&aring; fr&aring;gan nedan f&ouml;r att forts&auml;tta utforska webbplatsen.<br /><br />
                ";
        }
        ?>
    
        <span style="font-size: medium;">&Auml;r din konsol ink&ouml;pt i Sverige?</span> 
        <ul>
        <!--set_region.php?region=pal-->
        <li><a title="Ja, den &auml;r ink&ouml;pt i Sverige" href="javascript:SubmitPal();">Ja, den &auml;r ink&ouml;pt i Sverige</a>.</li>
        <li><a title="Nej, den &auml;r ink&ouml;pt i ett annat land inom Europa" href="set_region.php?region=pal">Nej, den &auml;r ink&ouml;pt i ett annat land inom Europa</a>.</li>
        <li><a title="Nej, den &auml;r ink&ouml;pt fr&aring;n Japan" href="set_region.php?region=ntsc-j">Nej, den &auml;r ink&ouml;pt fr&aring;n USA / Kanada</a>.</li>
        <li><a title="Nej, den &auml;r ink&ouml;pt fr&aring;n Japan" href="set_region.php?region=ntsc-j">Nej, den &auml;r ink&ouml;pt fr&aring;n Japan</a>.</li>
        <li><a title="Nej, den &auml;r ink&ouml;pt fr&aring;n ett annat land" href="set_region.php?region=unknown">Nej, den &auml;r ink&ouml;pt fr&aring;n ett annat land</a>.</li>
        </ul>
    
        <?php
        include_once "page_bottom.html";
    }
    else
    {
        $_SESSION["region"] = $_GET["region"];
        if (isset($_SESSION["returnto"]))
        {
            $hdr = "Location: " . $_SESSION["returnto"];
            header($hdr);
        }
        switch ($_POST["region"])
        {
            case "pal": 
                header("Location: pal.php"); break;
            case "ntsc":
                header("Location: ntsc.php"); break;
            case "ntsc-j":
            case "unknown":
                header("Location: region_mismatch.php");
                break;
        }
    }
    ?>
    Out of the hyper-links, I want them to pass the info "region" to the script with one of the following values:
    - pal
    - ntsc
    - ntsc-j
    - unknown

    The first two hyperlinks should send pal, the third ntsc, the fourth ntsc-j and the last unknown.

    Like I said, I have tried a number of approaches. These are:

    <a href="php_script.php?region=pal">...</a>
    This does nothing. The $_GET[] array is empty when the script loads again.

    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" id="submit_" method="post">
    <input type="hidden" name="region" value="ntsc" />
    <input type="submit" value="submit" />
    </form>
    document.forms["submit_"].submit();
    Same as above. This does nothing. $_POST[] array is empty.

    And finally the dynamic approach above by creating a form and submitting it. This also does nothing. Literally. It doesn't even submit, it looks like because the page doesn't refresh. No Javascript errors or anything.

    The strange thing is that clicking on a submit button works. I don't know why. Using any type of Javascript, it fails to work for me.
    Using hidden or text or whatever for the type doesn't matter at all from my tests.

    Does anyone have experience on how to make this work?

    EDIT: Get works just fine locally.
    Something amiss with the server?
    Last edited by Elysia; 07-30-2010 at 09:48 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    I'm not sure what's wrong. Filling in a link with the appropriate query string is a pretty usual way to pass data generated from JS to a php script. If the user clicks a link the parsed query string is available from $_GET['name']. Are you sure when you click this link the browsers address is set to something with an expected query string?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Even the full URL that it should produce:
    <No more links for you!>
    I did work fine locally before, but now it fails again. Even with the full address.

    EDIT:
    /smacks forehead
    Oops. It looks like it DID work.
    Only the page it redirected to redirected back :/
    Last edited by Elysia; 07-30-2010 at 10:13 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EMCA script and JavaScript
    By Aparavoid in forum General Discussions
    Replies: 11
    Last Post: 08-11-2009, 10:57 AM
  2. Professional JavaScript for Web Developers, 2nd ed.
    By neandrake in forum Programming Book and Product Reviews
    Replies: 2
    Last Post: 05-16-2009, 09:44 AM
  3. JavaScript book recommendations
    By neandrake in forum Tech Board
    Replies: 2
    Last Post: 04-05-2009, 12:27 PM
  4. Replies: 2
    Last Post: 03-10-2009, 08:36 PM
  5. Javascript: Error in FTP access
    By alphaoide in forum Tech Board
    Replies: 2
    Last Post: 05-10-2005, 07:13 AM