1

I have a form with several fields. The value of one of the hidden fields is passed in the URL along with some other input values like this:

example.html#firstname=Homer&lastname=Simpson&itype=TARGET

I have a javascript that parses the URL and that is populating the form just fine. The output of the form contains all of the values passed from the url, so I know the variable are getting passed to the form fields.

My issue is - I want to be able to setup an if statement to change the text of one of the headings based on the value of itype.

Here's my code:

        var itypestuff = document.getElementById("itype").value;

        document.write ("<p>The value of the variable is " + itypestuff.value + "</p>");

        if( itypestuff == "TARGET" ){
           document.write("This is the target");
        }

        else{
           document.write("This is not the target");
        }

I added the first document.write statement as a debug and it's coming back with: The value of the variable is undefined

Here's what the itype field variable looks like:

    <input type="hidden" name="itype" id="itype" value="">

Any ideas how I can get the variable from the URL into my javascript variable so I can do my if statement?

3
  • Try var itypestuff = document.getElementById("itype"); (yes, without the value there) Commented Jul 30, 2015 at 14:09
  • Why do you need to read the value from a hidden field if you are already touching the value in code when you strip it out of the url? Place it in a variable at that moment and then use it later in your processing of the page. This way you would totally skip messing with the DOM. Commented Jul 30, 2015 at 14:13
  • My code for stripping it out of the URL and assigning it to form variables is just a loop so it doesn't look at particular variables - it just separates them all and overwrites the empty values for the visible and hidden fields. Commented Jul 30, 2015 at 14:26

1 Answer 1

2

Change :

 document.write ("<p>The value of the variable is " + itypestuff.value + "</p>");

To

document.write ("<p>The value of the variable is " + itypestuff + "</p>");

OR

Change

var itypestuff = document.getElementById("itype").value;

To

var itypestuff = document.getElementById("itype");
// And 
if( itypestuff.value == "TARGET" ){ //...

EDIT

You have to change your html like that :

<input type="hidden" name="itype" id="itype" value="<?= htmlspecialchars($_GET['itype']); ?>">

And call : example.php?firstname=Homer&lastname=Simpson&itype=TARGET

Sign up to request clarification or add additional context in comments.

8 Comments

When I make either of those changes - the document.write statement comes back with a blank (ie The value of the variable is (nothing here))
Becauce value="" is null. Change by : value="TARGET" in your HTML.
The problem is - I want to pass the value of that form variable from the URL - so it will be TARGET sometimes - but will be MISSEDTARGET other times - I want it to be dynamic and not fixed. LIke I said - the output when I submit the form of itype is showing TARGET - but it's like it's not getting set in the timeline when the javascript is interrogating the form value.
Look my edits. I've added what you missing in your html.
When I do the PHP code - I get: The value of the variable is [object HTMLInputElement]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.