0

This is my php file in which I want to validate only numeric input for my text-boxes , with ids = Mobile & Home:

$elementids = array("Mobile","Home");
$serialized = rawurlencode(serialize($elementids));

$testvar='validatenumbers.php?elementids='.$serialized ;



<script type="text/javascript"  src="only-numbers.php?elementids=<?php echo $serialized ; ?>"></script>

Then , the other file : validatenumbers.php :

$testvar = unserialize(rawurldecode($_GET['elementids']));

echo "<pre>";
print_r($testvar);
echo "</pre>";

It displays nothing , i.e. there is nothing in the '$testvar' array.

----EDIT------ When am checking $_GET, then it shows :

$_GET['elementids'] ---- a:2:{i:0;s:24:\"Mobile\";i:1;s:20:\"Home\";} 

   rawurldecode($_GET['elementids']) ---- a:2:{i:0;s:24:\"Mobile\";i:1;s:20:\"Home\";}


   unserialize(rawurldecode($_GET['elementids']))  = **empty**
2
  • How is validatenumbers.php called? from browser? via include()? What is the relationship between you current PHP file and validatenumbers.php? Commented Nov 14, 2011 at 8:12
  • @Moe Sweet : yes, validatenumbers.php is called from other php file , the first code is of that file Commented Nov 14, 2011 at 8:23

2 Answers 2

0
  1. you don't have to urldecode $_GET values
  2. there ought to be an HTML code in your question. Without it it makes a little sense.
  3. http_build_query()
2
  • sir, actually , i have many forms for my website, now in each and every form i have to validate only numeric input , using javascript, so i have to copy the same code, in each file, so , to get rid of this ,am creating a common file, by which i will receive the Element IDs and create a javascript to validate numeric input only Commented Nov 14, 2011 at 8:19
  • Why don't you just put in an additional attribute like <input type="text" validate="int" ... >, then write the javascript to look for all the input boxes with the attribute of "validate" set to "int". You do not need to use PHP to generate the javascript then. Commented Nov 14, 2011 at 8:28
0

2 things.

1) If you want to put stuff on the url, you need to encode it using the urlencode function in php. (http://php.net/manual/en/function.urlencode.php) otherwise, the varibels values might contain characters that the browser might treat differently.

2) There is also a limit on the length of the url. If your array serializes into a very long string, some browsers might truncate it.

But since your sample array is only 2 element, I think you should start by calling urlencode on the value and see if it works.

<script type="text/javascript"  src="only-numbers.php?elementids=<?php echo urlencode($serialized) ; ?>"></script>

----------EDIT----------

Opps, just noticed that you did encode. :(

Then the problem lies with the urldecode on the target file. As php already decoded it for you. OIf you decode it again, you might lose some characters.

8
  • when am doing this : unserialize($_GET['elementids']) ----then it gives an empty string.. Commented Nov 14, 2011 at 8:21
  • Have you tried just printing out the value of "$_GET['elementids']" and see if there are values there? Commented Nov 14, 2011 at 8:25
  • It might be a case of the browser acting smart and filtering out some of the characters. Commented Nov 14, 2011 at 8:26
  • $_GET['elementids'] ---- a:2:{i:0;s:24:\"Mobile\";i:1;s:20:\"Home\";} Commented Nov 14, 2011 at 8:28
  • Looks valid.... But I notices that there are slashes in the values. Is your php set to automatically strip slashes. It usually also cause some problem. I would usually set it to off so. Commented Nov 14, 2011 at 8:33

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.