![]() |
| | #1 |
| The Beautiful C++ Utopia Join Date: Oct 2007
Posts: 16,455
| Some Javascript help 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ått ut. Var vänlig svara på frågan nedan för att fortsätta utforska webbplatsen.<br /><br />
";
}
?>
<span style="font-size: medium;">Är din konsol inköpt i Sverige?</span>
<ul>
<!--set_region.php?region=pal-->
<li><a title="Ja, den är inköpt i Sverige" href="javascript:SubmitPal();">Ja, den är inköpt i Sverige</a>.</li>
<li><a title="Nej, den är inköpt i ett annat land inom Europa" href="set_region.php?region=pal">Nej, den är inköpt i ett annat land inom Europa</a>.</li>
<li><a title="Nej, den är inköpt från Japan" href="set_region.php?region=ntsc-j">Nej, den är inköpt från USA / Kanada</a>.</li>
<li><a title="Nej, den är inköpt från Japan" href="set_region.php?region=ntsc-j">Nej, den är inköpt från Japan</a>.</li>
<li><a title="Nej, den är inköpt från ett annat land" href="set_region.php?region=unknown">Nej, den är inköpt frå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;
}
}
?>
- 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 | |
| | #2 |
| Registered User 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 | |
| | #3 |
| The Beautiful C++ Utopia 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |