Read value of hidden input field from previous page

I have a hidden input area within a form on a page:


<form id="contactForm" method="post" action="../scripts/sendmail_cdo.asp" style="background:#E6E7E8; padding:10px;">
<div id="emailDiv">
<input type="text" name="email" autocomplete="off" value="">
</div>
</form>

On the page the form is submitted to, at the very top of the page I have my script, to check if the input has a value or not, just testing both to try and get it to work, and it doesnt seem to recognise my code, basically its not re-directing to any of the pages below


<script>
var val = document.getElementById('email').value;
if (/^\\s*$/.test(val)){
self.location="[contact/index1.asp](http://www.hobbsvalve.co.uk/contact/index1.asp)"; 
} else {
self.location="[contact/index2.asp](http://www.hobbsvalve.co.uk/contact/index2.asp)";  
}
</script>

Not sure what Im doing wrong.

Cheers

The value isn’t accessible from the DOM since it doesn’t exist there anymore, rather it will currently be existing in the POST array of the page (since that is how you sent the form). In PHP you would access it with something like var email = $_POST[‘email’] . In asp however, it might be something like Request.Form[‘email’] but I’m not entirely familiar with asp and it might depend on what server language you’re using.

If you want to do it entirely in Javascript though, you would move the validation onto the same page as the form and redirect there or do it via AJAX.

Sorry that PHP solution should be $email not var email. And I also noticed that you’re also trying to call the element by ID without setting the ID on the input element, which is something you’ll also have to do if you do the same page javascript validation solution.