Jump to content
MakeWebGames

Recommended Posts

Posted

Someone is teaching me PHP and he is not on right now.

He left me with this:

 

and if you want a script to do something when someone clicks a certain button youd go like this

<?php

switch($_GET['action'])

{

case 'testfunction': test_function(); break;

}

function Test_function()

{

if ($test==1)

print "Ze test works!!!";

else

die ("Ze test isn't working");

}

?>

then whenever the function opened if $test (a variable ) =1 it would say "Ze test works!!!" and if test equaledd anything else it would say "Ze test isn't working.

then you could change whats in the Print quotes to change the message and change the $test variable to change what variable its checking. you could also change the =1 to another number or something like > or != which means doesn't equal.

now to run this function you could go like this

print "<form action='file.php?action=testfunction' method='post'>

<input type='submit' value=test function' /></form>";

put that in your file (but outside your function) and it would make a button called "test function" and when clicked it would open the "test_function" function

 

 

Can someone help explain this? Thanks.

Posted

RE: Help

 

<?php
switch($_GET['action'])
{
case 'testfunction': test_function(); break;
}

function Test_function()
{
if ($_POST['test']==1)
print "Ze test works!!!";

else
die ("Ze test isn't working");
}

print "<form action='file.php?action=testfunction' method='post'>
<input type=hidden name=test value=0>
<input type='submit' value='Fail button' /></form>


<form action='file.php?action=testfunction' method='post'>
<input type=hidden name=test value=1>
<input type='submit' value='Win button' /></form>
";
?>

 

Stick that in a php file, run it. See what happens. Look at the code. Learn ;)

Posted

seanybob you left out a { and if you want a js popup, use this:

<?php
switch($_GET['action'])
{
case 'testfunction': test_function(); break;
}

function Test_function()
{
if ($_POST['test']==1)
echo'<script type="text/javascript">
function message()
{
alert("The test works.");
}
</script>
<body onload="message()">';
else if($_POST != 1)
{
echo'<script type="text/javascript">
function message()
{
alert("The test doesnt work.");
}
</script>
<body onload="message()">';
}
else
{
print "<form action='file.php?action=testfunction' method='post'>
<input type=hidden name=test value=0>
<input type='submit' value='Fail button' /></form>


<form action='file.php?action=testfunction' method='post'>
<input type=hidden name=test value=1>
<input type='submit' value='Win button' /></form>
";
}
?>
Posted

seanybobs code works fine, he didn't forget a " { ", it's just another way of using if's.

 

@OP

html/css/js files can be viewed locally without installing additional software, however if you want to execute php files (or files with php code and later on with a database interaction probably) you do need extra software installed. To achieve this, many people install wampserver, xampp or additional software. Once this is done you can execute php files, and run queries on your computer.

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.   Paste as plain text instead

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