Sulman Posted October 10, 2007 Share Posted October 10, 2007 HI I'm having problems extracting values from post vars. The post array looks like this (print_r($_POST)): Array ( [response-812] => Y-812 [notes-812] => [FixtureID-812] => 812 [response-814] => choose-814 [notes-814] => [FixtureID-814] => 814 [response-818] => choose-818 [notes-818] => [FixtureID-818] => 818 [response-819] => choose-819 [notes-819] => [FixtureID-819] => 819 [response-1522] => choose-1522 [notes-1522] => [FixtureID-1522] => 1522 [response-1524] => choose-1524 [notes-1524] => [FixtureID-1524] => 1524 [response-1527] => choose-1527 [notes-1527] => [FixtureID-1527] => 1527 [response-1528] => choose-1528 [notes-1528] => [FixtureID-1528] => 1528 ) (the first select has been chosen and no others) I am trying to loop through the array and find any that begin with Y, P or N (these are select boxes that are distinguished between by applying the fixtireID to the option). E.g.: <select name="response-812"> <option value="choose-812">Choose</option> <option value="Y-812">Yes</option> <option value="P-812">Provisionally</option> <option value="N-812">No - Reject</option> </select> <select name="response-814"> <option value="choose-814">Choose</option> <option value="Y-814">Yes</option> <option value="P-814">Provisionally</option> <option value="N-814">No - Reject</option> </select> (there are also some different post vars) But when I use the following code to check each var it doesn't seem to be working: foreach($_POST as $p) { $pos=strpos($p, "N"); $pos=strpos($p, "Y"); $pos=strpos($p, "P"); echo "pos=".$pos." for post var [".$p."]<br> "; } And just gives me this: pos= for post var [Y-812] pos= for post var [] pos= for post var [812] pos= for post var [choose-814] pos= for post var [] pos= for post var [814] pos= for post var [choose-818] pos= for post var [] pos= for post var [818] pos= for post var [choose-819] pos= for post var [] pos= for post var [819] pos= for post var [choose-1522] pos= for post var [] pos= for post var [1522] pos= for post var [choose-1524] pos= for post var [] pos= for post var [1524] pos= for post var [choose-1527] pos= for post var [] pos= for post var [1527] pos= for post var [choose-1528] pos= for post var [] pos= for post var [1528] Where I would expect this: pos=0 for post var [Y-812] pos= for post var [] pos= for post var [812] pos= for post var [choose-814] pos= for post var [] pos= for post var [814] pos= for post var [choose-818] pos= for post var [] pos= for post var [818] pos= for post var [choose-819] pos= for post var [] pos= for post var [819] pos= for post var [choose-1522] pos= for post var [] pos= for post var [1522] pos= for post var [choose-1524] pos= for post var [] pos= for post var [1524] pos= for post var [choose-1527] pos= for post var [] pos= for post var [1527] pos= for post var [choose-1528] pos= for post var [] pos= for post var [1528] Can anyone see what I am doing wrong? (or even recommend a better way?) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/ Share on other sites More sharing options...
darkfreaks Posted October 10, 2007 Share Posted October 10, 2007 <?php $pos= array_search('y',$p); $pos= array_search('p',$p); $pos= array_search('n',$p); echo "pos=".$pos." for post var [".$p."]<br> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-365999 Share on other sites More sharing options...
Sulman Posted October 10, 2007 Author Share Posted October 10, 2007 Thanks for your help. But foreach($_POST as $p) { $pos= @array_search('Y', $p); $pos= @array_search('P', $p); $pos= @array_search('N', $p); //echo "pos=".$pos."<br>"; echo "pos=".$pos." for post var [".$p."]<br> "; } just gives me pos= for post var [Y-812] pos= for post var [] pos= for post var [812] pos= for post var [choose-814] pos= for post var [] pos= for post var [814] pos= for post var [choose-818] pos= for post var [] pos= for post var [818] pos= for post var [choose-819] pos= for post var [] pos= for post var [819] pos= for post var [choose-1522] pos= for post var [] pos= for post var [1522] pos= for post var [choose-1524] pos= for post var [] pos= for post var [1524] pos= for post var [choose-1527] pos= for post var [] pos= for post var [1527] pos= for post var [choose-1528] pos= for post var [] pos= for post var [1528] Any idea why? Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366001 Share on other sites More sharing options...
darkfreaks Posted October 10, 2007 Share Posted October 10, 2007 <?php $pos= array_search('y',$p); $pos= array_search('p',$p); $pos= array_search('n',$p); echo "pos="; echo $pos; echo"for post var:"; echo $p; echo"<br> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366002 Share on other sites More sharing options...
Sulman Posted October 10, 2007 Author Share Posted October 10, 2007 Thanks Should that be within the foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366003 Share on other sites More sharing options...
darkfreaks Posted October 10, 2007 Share Posted October 10, 2007 yupp Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366004 Share on other sites More sharing options...
Sulman Posted October 10, 2007 Author Share Posted October 10, 2007 Thanks for your help so far. But that doesn't work either. Just gives me the same results as above. (ignoring the multiple errors: Warning: array_search() [function.array-search]: Wrong datatype for second argument in c:\Inetpub\wwwroot\club\handles\handle-update-fixtures.php on line n) ?? Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366005 Share on other sites More sharing options...
darkfreaks Posted October 10, 2007 Share Posted October 10, 2007 what are you trying to output? and why is array search supressed with @? Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366006 Share on other sites More sharing options...
Sulman Posted October 10, 2007 Author Share Posted October 10, 2007 Well my ultimate goal isn't to output anything. I'm just outputing at the moment for debug purposes. The idea is to loop through the post array and find any of the selection boxes that do not contain "choose" (as this is the default option and therefore means no optionhas been selected). Once I have found one that contains either a Y, P or N I can then extract the fixtureID off the end of it and therefore will know what has been chosen for each fixture. Hope that makes sense ??? Thanks! [edit] The errors are supressed as I keep getting "Warning: array_search() [function.array-search]: Wrong datatype for second argument in c:\Inetpub\wwwroot\club\handles\handle-update-fixtures.php on line n" Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366007 Share on other sites More sharing options...
darkfreaks Posted October 10, 2007 Share Posted October 10, 2007 <?php foreach($_POST as $p) { $pos=strpos($p, "N"); $pos=strpos($p, "Y"); $pos=strpos($p, "P"); if ($pos=="N") { echo $pos; } elseif ($pos=="Y") { echo $pos; } elseif ($pos=="P") { echo $pos; } }?> Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366011 Share on other sites More sharing options...
Sulman Posted October 10, 2007 Author Share Posted October 10, 2007 that just gives me a blank screen. I must be doing something wrong somewhere but it seems bizare that this isn't working given the array that the POST is chucking out. ??? Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366013 Share on other sites More sharing options...
darkfreaks Posted October 10, 2007 Share Posted October 10, 2007 read up on array_search() http://us2.php.net/manual/en/function.array-search.php Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366014 Share on other sites More sharing options...
Sulman Posted October 10, 2007 Author Share Posted October 10, 2007 thanks. I have looked at array_search but I don't think it's really what I need. As per my first post, I can loop through the $_POST array ok, it's just extracting the Y, N or P bits that doesn't seem to be working? Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366017 Share on other sites More sharing options...
Sulman Posted October 10, 2007 Author Share Posted October 10, 2007 this code is a simple debug: foreach($_POST as $p) { echo "checking ".$p." for Y, N or P---"; $pos=strpos($p, "N"); $pos=strpos($p, "Y"); $pos=strpos($p, "P"); echo "pos= ".$pos."<br>"; } and outputs this: checking Y-812 for Y, N or P---pos= checking for Y, N or P---pos= checking 812 for Y, N or P---pos= checking choose-814 for Y, N or P---pos= checking for Y, N or P---pos= checking 814 for Y, N or P---pos= checking choose-818 for Y, N or P---pos= checking for Y, N or P---pos= checking 818 for Y, N or P---pos= checking choose-819 for Y, N or P---pos= checking for Y, N or P---pos= checking 819 for Y, N or P---pos= checking choose-1522 for Y, N or P---pos= checking for Y, N or P---pos= checking 1522 for Y, N or P---pos= checking choose-1524 for Y, N or P---pos= checking for Y, N or P---pos= checking 1524 for Y, N or P---pos= checking choose-1527 for Y, N or P---pos= checking for Y, N or P---pos= checking 1527 for Y, N or P---pos= checking choose-1528 for Y, N or P---pos= checking for Y, N or P---pos= checking 1528 for Y, N or P---pos= Surely the highlited line should have a pos? Mightily confused! Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366018 Share on other sites More sharing options...
Sulman Posted October 10, 2007 Author Share Posted October 10, 2007 Think I may have jsut spotted a school boy error... $pos is going to get overriden. so unless Y, N or P is the last position it wont show it... Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366026 Share on other sites More sharing options...
Sulman Posted October 10, 2007 Author Share Posted October 10, 2007 Oops. Yes that was it. thanks for your help darkfreaks. Solved Quote Link to comment https://forums.phpfreaks.com/topic/72579-solved-problem-trying-to-read-multiple-post-vars/#findComment-366033 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.