开发者

Strange JavaScript, PHP and Google Maps behavior

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_开发者_JAVA技巧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? :)


Replace $rout_markers with $chosen_markers


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>


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜