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å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;
}
}
?>
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?