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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

//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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.