redarrow Posted March 6, 2007 Share Posted March 6, 2007 For some reason this code wont display the results please help cheers. <?php $key=$_POST['key']; $val=$_POST['val']; if(isset($_POST['submit'])){ echo"Value is: $val <br> Key is: $key"; }else{ echo"not set sorry"; } ?> <form method="POST" action="testcode.php"> <?php $x=array(window => "window seal", dog => "bone", car => "wheels"); foreach($x as $key => $val){ ?> <input type='radio' name='<?php echo $key; ?>' value='<?php echo $val; ?>'> <?php echo $key; ?><br> <?php } ?> <br> <input type="submit" name="submit" value="GET IT!"> </form> Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/ Share on other sites More sharing options...
trq Posted March 6, 2007 Share Posted March 6, 2007 This line.... $x=array(window => "window seal", dog => "bone", car => "wheels"); should be.... $x=array("window" => "window seal", "dog" => "bone", "car" => "wheels"); unless you have defined the constants window,dog and car somewhere. Also note that your first two lines will generate a warning if your form has not yet been submitted. Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201115 Share on other sites More sharing options...
redarrow Posted March 6, 2007 Author Share Posted March 6, 2007 still blank just the echoed words weried? <?php echo"Value is: $val <br> Key is: $key"; ?> <form method="POST" action="testcode.php"> <?php $x=array("window" => "window seal", "dog" => "bone", "car" => "wheels"); foreach($x as $key => $val){ ?> <input type='radio' name='<?php echo $key; ?>' value='<?php echo $val; ?>'> <?php echo $key; ?><br> <?php } ?> <br> <input type="submit" name="submit" value="GET IT!"> </form> Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201130 Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 1 ) You do not have a form element with the name "key". Also you do not have a form element with the name "val". 2 ) If a user sees a group of radio buttons they expect that selecting one will unset the others (standard UI guidline) To achieve this they all need the same name, say, "name" 3 ) to process, the single selected value is returned in $_GET['name'] 4 ) if you want multiple selections, use checkboxes Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201131 Share on other sites More sharing options...
redarrow Posted March 6, 2007 Author Share Posted March 6, 2007 //how to convert this statement to the tandary operator please cheers example ?$name FALSE "choose one please"; if($name==FALSE){echo"please chose one <br> ";} <?php //how to convert this statement to the tandary operator please cheers if($name==FALSE){echo"please chose one <br> ";} if(($_POST['submit'])) { echo"Ansaw is: $name "; }else{ echo"not set sorry"; } ?> <form method="POST" action="testcode.php"> <?php $x=array("window" => "window seal", "dog" => "bone", "car" => "wheels"); foreach($x as $key => $val){ ?> <input type="radio" name="name" value='<?php echo $val; ?>'> <?php echo $key; ?><br> <?php } ?> <br> <input type="submit" name="submit" value="GET IT!"> </form> Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201190 Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 I think tou mean "ternary" operator <?php //how to convert this statement to the tandary operator please cheers if (isset($_POST['submit'])) { if (!isset($_POST['name'])) echo "please choose one <br> "; echo (isset($_POST['name'])) ? "Answer is: {$_POST['name']} " : "not set sorry"; } ?> <form method="POST" action=""> <?php $x=array("window" => "window seal", "dog" => "bone", "car" => "wheels"); foreach($x as $key => $val){ ?> <input type="radio" name="name" value='<?php echo $val; ?>'> <?php echo $key; ?><br> <?php } ?> <br> <input type="submit" name="submit" value="GET IT!"> </form> Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201212 Share on other sites More sharing options...
redarrow Posted March 6, 2007 Author Share Posted March 6, 2007 thank you but you added the ternary to a echo statement any reason cheers. more confusing now? echo (isset($_POST['name'])) ? "Answer is: {$_POST['name']} " : "not set sorry"; Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201221 Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 The ternary operator is used to replace an if/else. That's the only one there was. Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201225 Share on other sites More sharing options...
redarrow Posted March 6, 2007 Author Share Posted March 6, 2007 Thank you for your time and example can you tell me if you echo the varable with braces {} will save me using ".." method is that true. thanks agin. Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201231 Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 Instead of echo "Answer is: {$_POST['name']} "; you could use echo 'Answer is: ' . $_POST['name']; Is that what you mean? Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201241 Share on other sites More sharing options...
redarrow Posted March 6, 2007 Author Share Posted March 6, 2007 i want to use the brace method you provided but i need to no is it as powerfull as the '..' method to echo varables. Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201243 Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 When I run this script, the option without the {} is about 3 times faster. <?php if (isset($_GET['action'])) { $t1 = microtime(true); for ($i=0; $i<1000; $i++) { $a .= "Using braces {$_GET['data']}"; } $t2 = microtime(true); for ($i=0; $i<1000; $i++) { $b .= 'Not braces ' . $_GET['data']; } $t3 = microtime(true); echo 'With: ', $t2-$t1, '<br>'; echo 'Without: ', $t3-$t2, '<br>'; } ?> <form> <input type='text' name='data'> <input type='submit' name='action' value='Submit'> </form> Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201258 Share on other sites More sharing options...
redarrow Posted March 6, 2007 Author Share Posted March 6, 2007 did u just make that code in minuites. so braces are not as good as normall caternation then lol. playing ur game's well good. Link to comment https://forums.phpfreaks.com/topic/41516-varable-problams-cheers/#findComment-201259 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.