Jump to content

varable problams cheers.


redarrow

Recommended Posts

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

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.

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>

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

//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>

 

 

 

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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.