/    Sign up×
Community /Pin to ProfileBookmark

Varibale via Radio Buttons

I have two variables in a PHP script and one needs to be choosen by the user. I have scrapped together the below but not sure what excatly to set as the if statement. How do I call what the radio button is and its value, and how do I set it in the if statement?

I would also like the script to print the form if there is no variable set at all. $input should be the entry of a textbox.

[code=php]
$source = file_get_contents($input);
$output = htmlspecialchars($source);

// what should I set this statement as?
if

//linewrap
echo nl2br($output);

else

//no linewrap
echo “<pre>n $output n</pre>”
}
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsJun 30.2004 — i don't use radio buttons much with php, you could try this an figure it out:[code=php]<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if($_POST)
{
echo "<pre>";
print_r($_POST);
echo"</pre>";
}
?>
<form action="" method="post">
<input name="radio1" type="radio" value="1" checked> radio 1<br>
<input type="radio" name="radio2" value="2"> radio 2<br>
<input type="radio" name="radio3" value="3"> radio 3
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@DaiWelshJun 30.2004 — I gues you mean the user picks what format they want from radio buttons? If so

[code=php]
<input type="radio" name="linewrap" value="1" checked>
<input type="radio" name="linewrap" value="0">
[/code]


Then do

[code=php]
if(isset($_POST['linewrap'])) {
if($_POST['linewrap'] == '1') {
//linewrap
echo nl2br($output);
} else {
//no linewrap
echo "<pre>n $output n</pre>";
}
}
else{
// show form
}
[/code]


By naming the two radio buttons the same they form a group and the browser will only allow one of the two to be selected. The isset() will check that one or other is set (though since one is pre-selected there should really always be one or other set when form is submitted)

and then the value will either be 0 or 1.

HTH,

Dai
Copy linkTweet thisAlerts:
@GavinPearceauthorJun 30.2004 — Yea that's excatly what im after, its just what the IF statement shoudl be that was confusing me. OK so it juch checks values like a text box.

Thanks loads!
×

Success!

Help @GavinPearce spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...