0

I am breaking my brain in this situation :)

I have a form:

<form method="post" action="">
   <input type="hidden" name="entered_markers"
   value="<script type='text/javascript'> document.getElementById('rout_markers').value; </script>" />
 <input type="submit" value="Enter the trees you saw!" />
</p>
</form>

As you can see, the entered_markers tries to pass some JavaScript variables.

When I process the request, I do this

$chosen_markers = $_POST['entered_markers'];

Then the strange part :)

if ( empty ($chosen_markers) || !isset($chosen_markers) )  {
      $errors[] = 'Please click on the map to select spots where you spotted these tree. Markers: '.$chosen_markers;
} else {
   // Set something to signify that things are ok
}

And I always have the result that the validation thought the input was not empty, but when I tried to use that variable $rout_markers it just has nothing in it.

Where am I going wrong here? Isn't it sort of a strange thing that is happening? :)

1
  • What is $rout_markers? Commented Mar 31, 2011 at 13:12

4 Answers 4

2

Replace $rout_markers with $chosen_markers

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

1 Comment

Sorry that was typo in the question. In the code it was the right way.
1

Try it by creating a JavaScript function within your head and pass the form as the parameter to parse it's input fields. I went on and created a dummy text field name "rout_markers" and gave it a value of 300. So, on your PHP side if you look for $_POST['entered_markers'] it would echo out to be 300 if you use the example below:

<html>
<head>
<script type='text/javascript'>
    function submitCoor(form){
        form['entered_markers'].value = document.getElementById('rout_markers').value;
    }
    </script>
</head>
<body>
<input type='text' value='300' id='rout_markers' />
<form method="post" action="test.php" onsubmit="submitCoor(this)">
    <input type="hidden" name="entered_markers"
    value="" />
    <input type="submit" value="Enter the trees you saw!" />
</form>
</body>
</html>

Comments

1

Try this:

<form method="post" action="" onsubmit="document.getElementById('entered_markers').value = document.getElementById('rout_markers').value;">
    <p>
        <input type="hidden" name="entered_markers" id="entered_markers" value="" />
        <input type="submit" value="Enter the trees you saw!" />
    </p>
</form>

Edit: and replace $rout_markers with $chosen_markers as suggested by webarto.

2 Comments

I did things the way you suggested and it got rid of the strangeness where now the validation is recognizing that the input is empty. But the problem is that there are values in the document.getElementById('rout_markers') element, but I just can't seem to get them. Any ideas how to actually pass them to the request so the values get passed to the request correctly? - Thank you
When is <input id="rout_markers"> value defined in your page? I updated my answer to change the value when the form is submitted.
1

The code below will probably explain what you could be doing wrong.

$errors = array();

if (
    isset($_POST['entered_markers']) // make sure the variable is available
) {

    if (
        is_string($_POST['entered_markers']) // make sure the data type is string (could be array when form is manipulated)
    ) {

        $markers = trim($_POST['entered_markers']); // trim whitespace and store it in a var

        if ($markers !== "") { // if the string is NOT empty

            echo "Input given!";
            // At this point you could add some more validation to check whether the given input is also what you expect it to be.
            // Preform a regexp for lat/lng for example.
            echo $markers;

        } else {
            $errors[] = "Parameter 'entered_markers' is empty.";
        }

    } else {
        $errors[] = "Parameter 'entered_markers' is not a string.";     
    }

} else {
    $errors[] = "Parameter 'entered_markers' is not found.";
}

print_r($errors);

Comments

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.