C Board  

Go Back   C Board > Community Boards > Tech Board

Reply
 
LinkBack Thread Tools Display Modes
Old 07-30-2010, 09:00 AM   #1
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
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?
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?

Last edited by Elysia; 07-30-2010 at 09:48 AM.
Elysia is offline   Reply With Quote
Old 07-30-2010, 09:58 AM   #2
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 497
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?
valaris is offline   Reply With Quote
Old 07-30-2010, 10:01 AM   #3
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
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 :/
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?

Last edited by Elysia; 07-30-2010 at 10:13 AM.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
EMCA script and JavaScript Aparavoid General Discussions 11 08-11-2009 10:57 AM
Professional JavaScript for Web Developers, 2nd ed. neandrake Programming Book and Product Reviews 2 05-16-2009 09:44 AM
JavaScript book recommendations neandrake Tech Board 2 04-05-2009 12:27 PM
C# and JavaScript, how can I get Javascript to use a C# object? mr_coffee C# Programming 2 03-10-2009 08:36 PM
Javascript: Error in FTP access alphaoide Tech Board 2 05-10-2005 07:13 AM


All times are GMT -6. The time now is 08:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22