Php True False Script - Im new and just curious how

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

Php True False Script - Im new and just curious how

BooZker
I have looked up just about every PHP tutorial. I just dont get the if else statements. I am still very new and understand HTML, CSS, XML, but this is so hard for me for some reason. I want a basic if else statement. What i want is something like:

"My name on trap 17 is BooZker

True | False"

When you click true it gives a alert saying congrats and brings you to the next page and if its wrong then it will start ALL over from the beginning after a alert box comes up saying sorry, try again.

Sorry about how easy this is, but i'm just not understanding PHP. I just need someone to write ONE question with that PHP and then i can figure it out from there. I just need have have an idea of how to even accomplish it.

Reply

QuickSilva
Ok i'm not sure what you mean by this. But if you don't understand the if/else statements i'll show you below.
Basicly the 'if' statement is like this 'if something do this'.
Example:
CODE

<?php
$var = "BooZker";
if ($var == "BooZker"){ //If the $var is equal to BooZker do this
echo '<script language="Javascript">
alert("The name is correct");
</script>';
}else{ //If it dosent equal it
echo '<script language="Javascript">
alert("The name is not correct");
</script>';
} //Ends the else
?>


Hope that puts you in the clear

Reply

shadowx
At a glance the code above looks good smile.gif So Boozker, take a look at it and see if you can work it out, if not then carry on reading!

The if/else statement isnt too complicated, it is basically:

CODE
If(this_thing == that_thing) {
then im going to do this stuff
...
...
...
} ELSE {
but if they arent the same im ging to do this stuff instead
...
...
...
};



As with all PHP statements and comands you must end it with the semi colon. The curly brackets basically mean "then" eg
CODE
IF(1 == 2) {
Ill do this
..
..
..
}


Would translate in human speech as
QUOTE
If one is equal to two then i will do this...


Thats what makes me understand complicated IF statements better. You can have NOT EQUAL to or GREATER THAN, LESS THAN, etcetera but dont worry too much about them just yet. If you apply this explaination to the code above it might make it easier to understand.

A good thing i used to get used to this was make a very simple log in script which would work similar to the code above but the variable $var would come from another page, if you wanted to try that out check http://www.tizag.com/phpT/postget.php to learn about those variables and then use the code that is above and simply change the line

CODE
$var = "BooZker";


for

CODE
$var = $_POST['username'];
(username can be any variable name, the link above will hopefully explain) and then is you type the right username in you will get one message, and if its wrong you get another message. Then you can add like a password field or something and change the messages etc and get the hang of it smile.gif


EDIT: Also see that instead of one EQUALS sign there are two in an IF statement, you always use two EQUALS signs in an IF because this means "is equal to" and one EQUALS sign means "give the value of"

EG
CODE
$var = "Bozker";    


Means

QUOTE
$var GIVE THE VALUE OF "Boozker"


and

CODE
IF(1 == 2)


means

QUOTE
if "1" IS EQUAL TO "2" ...


smile.gif

 

 

 


Reply

BooZker
Thanks so much shadowx for saying that. Makes me understand it a lot more. Thanks. Now i just got to keep on learning, but that made me understand a lot better.

Also Shining, if im not wrong isn't that script simply just a java script alert, and not a True | False? Is so how do i implement putting the word true or false?

Example would be:

1+1=2

True | False

And i want to know how to make a question that is false also. I dont want the script to always be True answers. Remember this is for a short true false quiz. I test that out though thanks. The alert is exactly what i wanted and i didnt even know you could put java script inside php thanks!

Reply

shadowx
Shining's code is a mix of php and JS, the JS is inside the ECHO command, so the php does the IF statement and then uses ECHO to make some JS to show the user.

I think the best way to use Shining's code wuld be to use a GET variable. EG you have two or more pages, say page one says "MY name is Boozker. TRUE|FALSE" and "true" and "false" are hyperlinks. Now they should link to the second page like this:

CODE

<a href=page_two.php?answer=TRUE>TRUE</a>|<a href=page_two.php?answer=FALSE>FALSE</a>


note the GET variable 'answer' at the end of the URL. Then on page two you would use code like:

CODE

$answer = $_GET['answer';


So now we have either "TRUE" or "FALSE" in our variable, and then use the IF function from shining and change the variable $var to $answer in the IF function and delete the first line. And change "Boozker" in the IF to ether "TRUE" or "FALSE".

That will then show the message you want if its true or false and then after the end of the IF you can either use ECHO to build the rest of the HTML or end the php with "?>" and start html with "<HTML>" and write the HTML that way.

And if you want false you can either write "FALSE" in the code or you can use the NOT EQUAL to code which is simply instead of two equals signs "==" you do an exclamation mark and an equals sign : "!=". So if you wanted to say:

IF the answer IS NOT true { i will do this }

the code is
CODE
IF($answer != "TRUE")
{do this ... ... ... ...};


and of course you can expand this code with the ELSE part too and that will be run if the answer IS true. So whereas in a normal IF TRUE function like shining wrote, the first lines are done if something *is* true in a "NOT TRUE" function like the one above, the first few lines are done if the IF statement is *not* true. So things are reversed, that can be confusing sometimes. Does that help?

Reply

BooZker
Awesome thanks so much. OK so just a couple questions about that. For stuff to add on, but it works great the way it is. I understand the if else statements now! It's a miracle.

OK so here are my questions

How could i put this?

CODE
if($answer != "TRUE")
{<center>Want to try again? <br /><br /> <a href="index.php">Yes</a> <a href="http://google.com">No</a></center>};
?>


obviously thats wrong, but how could i do that? OR how could i send them to another page, for example, you get the wrong answer you get sent to the retry.php and if you get it right you go on to the next page so page_2.php?

Next question is what was the:

CODE

$answer = $_GET['answer';


for? I put it in and it would never work, but when i didnt put it in it worked fine. Am i supposed to put it in?

Thanks for all the help guys. I know know how to make multiple choice tests also.

Reply

shadowx
QUOTE
How could i put this? How could i put this?

CODE
if($answer != "TRUE")
{<center>Want to try again? <br /><br /> <a href="index.php">Yes</a> <a href="http://google.com">No</a></center>};
?>


Well its almost right except to send out HTML or any text you need to use either

CODE
echo "<center>Want to try again?etc etc...";


Or

CODE
print "text here...";

I normally use echo and according to the php manual site ECHO is slightly faster so id reccomend using that. In which case your code would be like this:

CODE

if($answer != "TRUE")
{


echo "<center>Want to try again? <br /><br /> <a href='index.php'>Yes</a> <a href='http://google.com'>No</a></center>";


};
?>


See the echo command and the double quotes enclosing the HTML. Also its very important that you remove all double quotes from within the text, eg you had double quotes, and rightly so, around the page filename in the hyperlinks but when ECHO finds a double quote it just stops and buggers the whole page up! So instead i replaced them with single quotes, you can also completely remove them.

If you really really need the double quotes to be displayed then you can use a backslash \ just before the double quotes eg:

CODE

echo "<a href=\"somewhere.html\"> somewhere </a>";


And that would be fine.

Now about redirecting someone to a different page, there is a method in php but the drawback is that you cant use any echo or print or most other commands before it. The best way i think is to use javascript, im not very god with JS so i use codes i find on google for that! Just search for a javascript redirect and use an echo command to send it to the browser remembering to deal with the double quotes!

There is also one more method for changing the page, you can use an INCLUDE function. All this does it take the code from one file and run it, so if you had two pages the user can access the first and then the INCLUDE function will open the second, take the code from inside and run it as if it was the first page.

In your code i would use something like this:

CODE
<?

if($answer != "TRUE"){
    
    include("wrong.htm");
    
}ELSE{
    
    include("right.htm");
};


?>


if they get the answer wrong the first INCLUDE will run and it will load the file "wrong.htm" you can change this to whatever page you wanted, maybe a page saying you got it wrong start again or something.

if they get it right the second INCLUDE will run and it will load the file "right.htm" again you can change this, possibly so that it loads the next question.

the important thing to remember is that using INCLUDE the original file contents dont disappear, so if you include page 2 from page 1 then you will have code from both pages, and then you include another you will have 3 files worth of code, it wont save like this but it might make loading times longer for that user.



Overall for this example of the quiz i would use the javascript idea but in the future for things like making a log in system or things like that the INCLUDE idea is very usefull and i think a lot of content management systems use INCLUDE to load pages.


Hope that isnt confusing!


And the

CODE
$answer = $_GET['answer'];


is to set up a variable. Some places wont take a variable from the URL. So for example on my website i use a variable called "module" in my url like : "modules.php?module=something " and in some places my code will automatically use that variable but in other places i have to tell it t use that variable by using code like you asked about above.

$_GET simply means look for a variable in the URL of this page.

['answer'] Means look for the variable answer in the URL

So the code above is saying $answer EQUALS the variable named "answer" in the URL.

Not all hosts need this line but i think on my account here i needed to add it. Im not sure if you realized but there was a bracket missing from the code when you posted it, if thats how it is in your page then adding the bracket in like above should make it work.

Sorry this post is so long!!


Reply

QuickSilva
I did a bit of researching for you and found this on Hotscripts.
This is the URL:
http://www.hotscripts.net/PHP/Scripts_and_...zzes/index.html

I would recommend using one of those instead of making your own, as by the look of it, it looks pretty complex.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords :

  1. How To Make Php Newsletter Script
    (3)
  2. Php Guest Online Script
    (3)
    make an index.php copy and paste this code CODE <?php $db_host = "localhost";
    $db_user = "root"; $db_pass = ""; $db_name = "test";
    $dbc = mysql_connect($db_host, $db_user, $db_pass); $dbs =
    mysql_select_db($db_name); $tm = time(); $timeout = $tm -
    (30*60);
    if($_SERVER["REMOTE_ADDR"]){$ip=$_SERVER["REMOTE_ADDR
    "];} else{$ip=$_SERVER["HTTP_X_FORWARDED_FOR"];}....
  3. How To Make A View New Post Script?
    (5)
    Ok so i'm still working on the forum software i posted about a while back, but I have no idea
    how to do this. I want to make a view new post script, as this is one of the main things that my
    forum software dose not have that all other forums have. so does any body have an idea on how i
    would do this? Thanks.....
  4. Guessing Php Script
    (2)
    I am looking for: freeware php quess the person in the photo game script....
  5. Need Help Installing Dolphin Community Script!
    (5)
    I'm not sure if this is the right place to post this but I really need help in installing the
    dolphin community script. I have absolutely no previous experience of scripts or programming. I
    would really appreciate if someone could walk me through it step-by-step, or even do it for me by
    logging into my cpanel. I have tried to install it my self but I'm a little confused. I'm
    sure it won't take very long at all for someone who has done this before.....
  6. How Do I Connect To Live Database With Php Script?
    while being hosted with ComputingHost (6)
    I am not new to programming. I want to create a form to add some values into my tables, the code
    are all working. But I am not sure what is the URL to connect to my site's database. All along,
    I have been testing through MAMP, which provides a local copy of mySQL. Can anyone lend me a hand?
    My site's URL is http://limetouch.com/ ....
  7. Download Script For Mp3 Files
    (0)
    Hello, I'm looking for a download script for sound files (e.g. mp3, avi, wma, and other ones).
    i have found a few download scripts but they would not work for sound files for some reason. also
    this will not be used for allowing downloading of illegal or riped music, what i will be using this
    script for is i'm making a site for my church and the pastor wants to be able to recored the
    services and then have me upload them to the site so that the church members can download them for
    what ever reason. If some one could tell me how to make one or could show me a plac....
  8. Php Rediret Script
    (12)
    Ok, what I am trying to do is this. Re-direct a domain name called: avalon.asn.au to
    preschool.stmarksavalon.org.au I have created a script that will re-direct within the a folder.
    However, the avalon.asn.au and stmarksavalon.org.au are PARKED Domains. Any ideas on how to create
    this PHP Redirect Script please?....
  9. Forum Script
    (3)
    Hello, i'm wanting to start making my own forum software but i dont know where to start or what
    i need to know in order to do this. I know i will need php and mysql but what else, and could some
    one point me to a good site were i could learn php and mysql. Thanks ....
  10. How Would I Go About Making A Simple "counting" Script?
    (3)
    I plan on making a script for basic voting between different options, and I'd like to know what
    PHP coding I would require. Basically, each choice will be as simple as this: CODE <form
    method="post" action="process.php"> Best falsetto?<br><br>
    <input type="radio" name="1"> Person A<br> <input
    type="radio" name="2"> Person B<br> <input type="submit"
    value="Submit"> </form> What PHP would be used to basically add 1 value to a....
  11. Library Script
    Where? (6)
    Hello, everyone. Anyone knows where I can get a library script that acts like CMS script software,
    you can add books or delete them. I want to build virtual online library which can be accessible to
    everyone. Or just give me some advices how to make it build. I'm a novice in programming.....
  12. Script Not Working
    I don't know why. (6)
    For some reason my random string script is not working. I got a fatal error when I tried it under
    XAMPP. I do not know why. It looks syntatically correct. Could someone help me? Here is the script:
    (Warning its over 100 lines long) //This PHP script will generate a random array and turn it into
    a string consisting of 0-9 and A-Z. // This is the first developmental version. //Create 10 item
    array for string $string = array(0,0,0,0,0,0,0,0,0,0); //Create function to replace 10-36 with
    A-Z function conToStr() { for ($a = 0;$a switch($string ) { ....
  13. Script Help Required: Undefined Variable
    A fault I cannot spot in PHP (3)
    Hi, when running a PHP script I keep getting the error: QUOTE Notice: Undefined variable: bret
    in c:\program files\easyphp1-8\home\poll.php on line 294 Notice: Undefined
    variable: bret in c:\program files\easyphp1-8\home\poll.php on line 294 (And,
    yes, I get it twice). The code related to the variable is as follows: CODE function
    LogString($string,$type)     {         $t_log = "\n";
            $t_log .=
    $this->globaldata->server_vars['REMOTE_ADDR']."....
  14. Php Downloads Script
    (4)
    I've been looking all over the net for a PHP script which can provide an interface to browse a
    downloads database. The database could be powered by MySQL. If you know a script like this, please
    post it here. Thanks in advance, Ironchicken.....
  15. Will This Code Work
    php linking script ?p= (5)
    hi i'm not that great at php so i'm not to sure if this will work or not. but what i want to
    do is be able to use ?p=staff or what ever page name, with out the php extion, and i would like to
    no if this simple script i made would work. the code is: CODE <?php $p =
    $_GET['p']; if ( !empty($p) &&
    file_exists('./' . $p . '.php') && stristr( $p, '.'
    ) == False ) { // pages = directory where you store your pages    $file = './'
    . $p . '....
  16. Php Sessions And Post Variables Issues
    My script dosent seem to work as intended (1)
    You can test it out for yourself at http://sonesay.trap17.com/application.php I've been
    working on this page locally and it seems to be working fine but when I upload it to my trap17
    account the post variables dont get saved properly. Fill in some fields and submit it, the form
    will come up as a empty field yet when you resubmit it without any modifications and the data you
    entered in orginally will now magically appear, resubmit it again and it will be gone. This is
    really annoying as I have no clue why it would be doing this when it seems to work fine locally.....
  17. Html Code Tester. Online Script
    (15)
    Yes, yes. I have another script that I have written and I am distributing. I am not entirely sure if
    this works. I have not tested it yet, but I will later and post back with a demo and fix it up.
    Current script: CODE <?php //Save this as something like htmltest.php function
    CheckForm() { $html_unsafe=$_POST['code']; //Gives us our user
    input $html_safe=str_replace("<?php"," ",$html_unsafe);
    //Starts security measures $html_safe=str_replace("?>","
    ",$html_sa....
  18. Awesome Source Code Viewer Script
    (7)
    Hello! I have just came up with a sweet script to show the source code of any website and it
    only requires one file. This is the basis of the script and can be customized with CSS and other
    things and can be instituted as a public resource. Well I will provide the code and a step-by-step
    tutorial on each of its parts. This code has been tested by me. Enjoy! CODE <?php
    //This little tag starts our php script and is easily the most important part of the script. //We
    will start our base script here. //You can change some of the styles used here to your des....
  19. Is This A Good Script?
    A login script (9)
    Okay, I am trying to password one page of my website. I need confirmation if this is a safe code or
    not. The whole code is on the page I'm protecting. CODE <?php
    include('header.php') ?> <?php // Define your username and password
    $username = "THE_USERNAME"; $password = "THE_PASSWORD"; if
    ($_POST['txtUsername'] != $username ||
    $_POST['txtPassword'] != $password) { ?>
    <h1>Login</h1> <form name="form" method=&....
  20. Creatting A Playlist Through Php
    script help needed (5)
    Hi I am trying to make a script so that i can insert songs into a playlist, but i need a script in
    which it opens the playlist file and removes the closing tag at the end, so before i can add more
    entrys. e.g CODE <atx> <entry>Location 5</entry> <entry>Location
    4</entry> <entry>Location 3</entry> <entry>Location
    2</entry> <entry>Location 1</entry> <atx> But to add more entrys
    i would have to get rid of the atx, then use the fputs to place the new entry into the file. ....
  21. What Kind Of Script Do You Need ?
    post here and get free script (15)
    Hi everybody sorry if i posting here , i know I want design free PHP script and i dont know
    webmasters what kind of scripts want i think its better to aks here becuase trap17 is very nice
    webmasters forum So , Plz post here what kind of script with details you need ! sorry may en
    is not very well for example you need "upload center" : write "upload center" with upload center
    options ( like Ajax , Fast , multi lan and ... ) with this post we can give script details and
    webmasters idea /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.....
  22. Wappy Buddy V1.10 - Tibia Gold Edition By Wappy & Jon Roig
    the official wap download script (3)
    By downloading this script you are agreeing to the license and terms outlined below /biggrin.gif"
    style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> QUOTE /** * *
    @package: wappyBUDDY - Tibia Gold Edition * @version: 1.10 2006/10/01 00:00:01 wappy * @copyright:
    ©2003, 2006 jon roig, wappy * @release notes: this is the first official release of my download
    script despite pirate and incomplete copies floating around that were stolen from one of my previous
    servers. The next release will follow very shortly * @terms: wappyBUDDY is free softw....
  23. Free Auction Script
    Any Suggestions? (6)
    Any free auction script suggested? I want it to be as many practical functions as possible, yet
    easy to manage. And more importantly, it is free! Appreciate your kind suggestions!....
  24. Php Search Engine Script For Mysql Database
    (11)
    A search engine is provided to facilitate the user with undemanding and clear-cut search options.
    The search facility includes simple search, search by title, search by word/phrase, ect… Thus, the
    user is at a safe distance from the risk of selecting files/folders ambiguously. In addition, a
    history of recent searches can be preserved for future perusal. Now
    day’s visitors have a large option for his needs on internet and so visitors are not vesting their
    time by following the dead links on your site. So a search engine is essential for your....
  25. Wappychat_oldskool
    old version of my wap chat script :-) (15)
    here is a very old version of my wap chat script, its not very advanced but has privates, smileys
    etc. I will post some further versions (with owner, admin, mod status and profiles) when i have time
    to write the readme/install instructions for them. You will find instructions inside the zip. If you
    have any problems post here but i know it don't work on all servers for some reason but it does
    work on the trap server so will be cool ok /tongue.gif" style="vertical-align:middle" emoid=":P"
    border="0" alt="tongue.gif" /> ....
  26. Script That Tracks The User Status
    how can I track on or offline users? (4)
    long explaination: hey, I'm building a user profile site right now. And, I kinda know how to
    make a online/offline detector, but not totally sure. I know I can make a mysql database to track
    them, but how does it entrer the information? I could easily put in a field where when they login it
    sets them to online, but if they don't sign out, and just exit the browser, how can I tell.
    short: I want someone to tell me how to make a online/offline status detector, like they have here
    on trap17. I'd be thrilled if you can post to this, thanks, arcticsnpr....
  27. Watermark Your Image With Simple Php Script
    found it on the net (35)
    This script was found on the net http://tips-scripts.com/?tip=watermark#tip B&T's Tips &
    Scripts site. Just in case the site may not show, I will include the code here: List of things
    needed: 1. your image in any format 2. watermark image--in gif format with transparent background 3.
    script below with name (i.e. watermark.php) CODE <?php // this script creates a watermarked
    image from an image file - can be a .jpg .gif or .png file // where watermark.gif is a mostly
    transparent gif image with the watermark - goes in the same directory as this script // ....
  28. Transfer Variables To Another Php Script
    (8)
    Hello, I've one registration page where the users fills in their information, is it possible to
    trasnfer the things the fill in on the registration page to another script that does someting and
    returnes something to the first page like true/false and then the registration gives an error
    messange if the other php script returned false? Something like the script "activates" another
    script that does something and returnes the result back to the original script. Best Regards ....
  29. Parse: Error Unexpected T_lnumber
    php parse error when running script (4)
    Hi. I've just created a php script. The main object of the script is to delete some old files
    and replace it with a new file with some new content, effectively moving the contents from one file
    to another. These are the first 50 lines of the file: /* Calculate For The "A" Group - The
    Latest Games ID */ $a_B = 002; while(file_exists("a_" . $a_B . ".dat")) {
    $a_B++; } $new_page_contents = " " . $_POST . " " . $_POST . "
    include \"/home/cmatcme/public_html/footer.php\"; ?> "; $a_stream = fopen(&....
  30. Script: Php Jukebox
    A one file script! (4)
    This scripts is so simple, you dont need to edit ANY of it! All you have to do is make a folder
    called 'songs' and put some audio files in it. Here is the whole page, I named it index.php
    and put it in a folder called 'music': CODE <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>
    <title>PHP jukebox</title> </head> <body> <!-- ©2005 Craig
    lloyd. All rights reserved. Visit cragllo.com for more sc....

    1. Looking for Php, True, False, Script

Searching Video's for Php, True, False, Script
advertisement



Php True False Script - Im new and just curious how



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE