Jump to content

Need help with some PHP code


wtfsmd

Recommended Posts

I am making a script, the user submits the information into the form field then when they hit submit it makes a html file.
I am a complete newb at PHP I'm trying to teach myself with some help from Google and places like here.  :-\
(i cut out alot of the code here so it is easier to read)
[code]<?php
$data = "
Molten Core - $attunement";

$attunement = $_POST['attunement'];
$filename = "applicants/$char_name a lvl $char_level $char_class.html";
$fp = fopen("$filename","a");
fwrite($fp, $data);

fclose($fp);[/code]

$attunement is a check option box.

I wanted to know how i could make a if else for $attunement, such as if the user did not check the box, when it got displayed it would show like Not Attuned. Right now when they select the check box it shows up fine, but when they don't it shows up blank.  (or maybe there is a better way to do it)
[code]if ($attunement == yes) {
echo '$attunement';
} else {
echo '- No Onyxia attunement';
}[/code]

Here is the form field page [url=http://overdriveguild.com/apply.php]http://overdriveguild.com/apply.php[/url]
Here is where you see the output [url=http://overdriveguild.com/applicants/]http://overdriveguild.com/applicants/[/url]
Link to comment
https://forums.phpfreaks.com/topic/32270-need-help-with-some-php-code/
Share on other sites

This looks good to me, you simply want to get a response of some sort on attunement right??

If so, the below code should work, I only changed the ==yes part, if $attunement does not exist, the else portion will be parsed, if $attunement does exist, then the if($attunement) part will parse

if ($attunement ) {
echo '$attunement';
} else {
echo '- No Onyxia attunement';
}
[code]
<form action="<? $_SERVER['PHP_SELF'] ?>" method="post">
<input type="checkbox" name="cb" value="Testing" />
<input type="submit" name="submit" />
</form>
<?
if (isset($_POST['submit'])) {
@ $cb = $_POST['cb'];
if(isset($cb)) {
echo $cb;
} else {
echo 'You must check the box';
}
}
?>[/code]
You cannot specify an if/else statement or any other function inside a variable.

$data="if(blah blah){do this}"; will be interpreted as a string and as you said it has.

You have to place your if/else statement in the spot where you are echoing the $data variable.
[code]
if(!$data){
echo 'Nothing Selected'
}else{
echo $data;
};[/code]

The highlighted spot in the screenshot is where you will need to put the if/else code


Side Note: Your Today's Date field in your form is not necessary, as you can use the date("Y-m-d"); to produce a date in the format of 2006-30-12 and make one less item the user has to input, and one less item you have to process.

[attachment deleted by admin]

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.