implement code

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 362 Views - Last Post: 05 May 2019 - 09:40 AM Rate Topic: -----

#1 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

implement code

Posted 03 May 2019 - 09:13 AM

hey guys i have a file calco.html and cal.ph....my question is how do i implement php code in html code??(i try to do include but it doesnet work)

html code
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>calculator</title>

</head>
<body>
	<?php include("cal.php"); ?>

	<form>
		<input type="text" name="num1" placeholder="enter number 1">
		<input type="tetx" name="num2" placeholder="enter number 2">

		<select name="operator">
			
			<option>None</option>
			<option>Add</option>
			<option>substract</option>
			<option>multply</option>
			<option>divide</option>

		</select>
		<br><br>

		<button type="submit" name="submit" value="submit">Calculate</button>
		<p>result is:</p>

	</form>

</body>
</html>





php code
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
	<?php
		if(isset($_GET['submit'])){
		$number1=$_GET['num1'];
		$number2=$_GET['num2'];
		$result=$_GET['operator'];
		switch($result)
		{
			case "None":
				echo "Error...you ned to selcet the oprator";
				break;

			case "Add":
					echo $number1+$number2;
					break;

			case "substract":
					echo $number1-$number2;
					break;


			case "multply":
					echo $number1*$number2;
					break;

			case "divide":
					echo $number1/$number2;
					break;




		}

	}


	?>

</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: implement code

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2287
  • View blog
  • Posts: 6,978
  • Joined: 15-January 14

Re: implement code

Posted 03 May 2019 - 10:25 AM

You're going to need to be more specific about what you're trying to do and about what "doesn't work" means. Say what it does or does not do.

One thing to note is that using include or require in PHP is pretty much the same as if you copied and pasted the code from one file into another file. So you shouldn't have a full document in your include file, if you include that then you're going to end up with another doctype, another head, etc in the middle of your other HTML document, and that's not valid HTML. If you want to include files containing HTML they should only be snippets of HTML to go in your parent page, not complete documents.

You've got a ton of spelling errors in your code also, that's just going to make things harder for you.

If that switch statement isn't printing anything, print out the value of $result to see what it is. You can use print_r($_GET) if you want to see the entire $_GET array.
Was This Post Helpful? 0
  • +
  • -

#3 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: implement code

Posted 03 May 2019 - 11:37 AM

i just wanna know how do i run thaht php code in html code...how combined this two code
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2287
  • View blog
  • Posts: 6,978
  • Joined: 15-January 14

Re: implement code

Posted 03 May 2019 - 01:17 PM

If you include the code then it will run. It's running, it's just not doing what you think it's doing. Other than printing too much HTML, have it print out $_GET to see exactly what you're working with.
Was This Post Helpful? 0
  • +
  • -

#5 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: implement code

Posted 03 May 2019 - 02:26 PM

yes but my problem ist that that this is my first program in php and im a total noob...my first was that calculator but i have all that code in same program and that work....now i hwould have separete this "two program"(html and php) and i dont know how php must be included in html that would be work
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5404
  • View blog
  • Posts: 14,305
  • Joined: 18-April 07

Re: implement code

Posted 03 May 2019 - 02:42 PM

Hi kretze, I see a few things that probably will help you a lot to know. First of all your PHP file "cal.php" doesn't need the HTML tags in it. You can and should just have the PHP code in it. Also, I am not sure if your server is setup this way on purpose, but if a filename ends with .html it is not seen as being PHP code and will not parse your include. Now if you have specifically setup your server to still pass .html files to PHP, you can ignore that statement. But usually you wouldn't want to do that.

So to reiterate my points and to propose the fixes to get you closer to a working solution you should...

1. Change cal.php to look like this...

<?php
    if(isset($_GET['submit'])){
        $number1=$_GET['num1'];
        $number2=$_GET['num2'];
        $result=$_GET['operator'];

        switch($result)
        {
            case "None":
                echo "Error...you ned to selcet the oprator";
                break;

            case "Add":
                echo $number1+$number2;
                break;

            case "substract":
                echo $number1-$number2;
                break;


            case "multply":
                echo $number1*$number2;
                break;

            case "divide":
                echo $number1/$number2;
                break;

        }

    }

?>



2. Change your calco.html file to be calco.php. That way when your web server runs the file, it will know to pass the file to your PHP interpreter for processing of the include statement.

Do keep in mind that the way you have this, you will see your statement up above the form and not down where you have the line "result is:". That is because it will echo in place of where the actual PHP is being included. If you would like to have more control over where the result is displayed, I would suggest you store the answer in a variable like "$result" and then you can echo that variable down where you want to put the answer.

I hope this helps you out. :)
Was This Post Helpful? 0
  • +
  • -

#7 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: implement code

Posted 03 May 2019 - 02:51 PM

Martyr2 thank you thank you...now works just fine and i know what i did wrong like i said this is my first php program and now working :)
Was This Post Helpful? 1
  • +
  • -

#8 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: implement code

Posted 04 May 2019 - 07:22 AM

so i just did like this and its working....so is this "right"? or is there better way to do it?? thank for answers

<button type="submit" name="submit" value="submit">Calculate</button>
		<p>result is:</p>
		<?php include("cal.php"); ?>


Was This Post Helpful? 0
  • +
  • -

#9 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: implement code

Posted 04 May 2019 - 09:01 AM

hey guys i have a question and i hope that you will help me couse i realy wont to know this stuff....so i lerning php with the book called "head first php and mysql" and i doing the "allien obduction" so i have 2 php file and when i try to run it i get (Fatal error: Uncaught Error: Function name must be a string in C:\xampp\htdocs\Html programi\allien\report.php:6 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Html programi\allien\report.php on line 6) that error and i dont know how to fix it...besides that i wondering if this code even works...so when i fix my error would send email or not?? thanks for answers i hope i didnt too annoying

allien.php
<!DOCTYPE html>
<html>
<head>
	<title>Alien obducted me...--report an obduction</title>
</head>
<body>
	<form method="post" action="report.php" >
		<label for="firstname">First Name:</label>
		<input type="text" id="firstname" name="firstname" /><br />
		<label for="lastname"> Last Name:</label>
		<input type="text" id="lastname" name="lastname" /><br />
		<label for="email">what is your Email addres</label>
		<input type="text" id="email" name="email" /><br />
		<label for="whenithappned">when did it happned</label>,
		<input type="text" id="whenithappned" name="whenithappned" /><br />
		<label for="howlong">how long did you gone</label>
		<input type="text" id="howlong" name="howlong" /><br />
		<label for="howmany">how many did you see</label>
		<input type="text" id="howmany" name="howmany" /><br />
		<label for="alliendescription">describe them</label>
		<input type="text" id="alliendescription" name="alliendescription" size="32" /><br />
		<label for="whatdidthey">what they did for you</label>
		<input type="text" id="whatdidthey" name="whatdidthey" size="32" /><br />
		<label for="fangSpotted">did you see my dog fang</label>
		Yes<input id="fangSpotted" name="fangSpotted" type="radio" value="yes" />
		No<input id="fangSpotted" name="fangSpotted" type="radio" value="no" /><br />
		<img src="fangs.jpg" width="100" height="175" align="my obducted dog fangs" /><br />
		<label for="other">anything else you wanto to add</label>
		<textarea id="other" name="other"></textarea><br />
		<input type="submit" value="report obduction" name="submit" />

	</form>

</body>
</html>





report.php


<?php

 $when_it_happned=$_POST('whenithappned');
 $how_long=$_POST('howlong');
 $allien_decription=$_POST('alliendescription');
 $fang_spotted=$_('fangSpotted');
 $email=$_POST('email');

 echo "thans for submmiting the form";
 echo "you were obducted" . $when_it_happned;
 echo "and you were gon ". $how_long .'<br>';
 echo 'Describe them: ' . $allien_decription . '<br />';
 echo 'Was Fang there? ' . $fang_spotted . '<br />';
 echo 'Your email address is ' . $email;


?>



Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6252
  • View blog
  • Posts: 24,024
  • Joined: 23-August 08

Re: implement code

Posted 04 May 2019 - 09:43 AM

Where you're using $_POST, it should be $_POST['whenithappned'], not $_POST('whenithappned'). When you use parentheses like that it thinks you're trying to call a function.
Was This Post Helpful? 0
  • +
  • -

#11 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: implement code

Posted 04 May 2019 - 09:59 AM

tnx jack that was helpful...now i have that problem Notice: Undefined index: whenithappned in C:\xampp\htdocs\Html programi\allien\report.php on line 6 and line 7 etc...

i try with include in allien.php in body but still doesent work...stil have a undefined index error
Was This Post Helpful? 0
  • +
  • -

#12 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: implement code

Posted 04 May 2019 - 04:29 PM

hey guys i did som improval on my php code and i dont know if its right or if its work correctly but now i have this(Notice: Undefined variable: when_it_happned in C:\xampp\htdocs\Html programi\allien\report.php on line 17
you were obducted and then line 18 etc...) error....can please anyone tell me what to do(i try google it and youtube it but for now nothing helpfull i didint find)..my report.php file now looks like that




<?php


if(isset($_GET["whenithappned"])&&($_GET["howlong"])&&($_GET["alliendescription"])&&($_GET["fangSpotted"])&&($_GET["email"]))
{
 $when_it_happned=$_POST['whenithappned'];
 $how_long=$_POST['howlong'];
 $allien_decription=$_POST['alliendescription'];
 $fang_spotted=$_['fangSpotted'];
 $email=$_POST['email'];
}

 echo "thans for submmiting the form";
 echo "you were obducted" . $when_it_happned;
 echo "and you were gon ". $how_long .'<br>';
 echo 'Describe them: ' . $allien_decription . '<br />';
 echo 'Was Fang there? ' . $fang_spotted . '<br />';
 echo 'Your email address is ' . $email;


?>



Was This Post Helpful? 0
  • +
  • -

#13 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: implement code

Posted 05 May 2019 - 07:57 AM

hey i post this post in my previus topic "implement code" but it has no responses so i try a new LUCK and start new topic:

hey guys i did som improval on my php code and i dont know if its right or if its work correctly but now i have this(Notice: Undefined variable: when_it_happned in C:\xampp\htdocs\Html programi\allien\report.php on line 17
you were obducted and then line 18 etc...) error....can please anyone tell me what to do(i try google it and youtube it but for now nothing helpfull i didint find)..my report.php file now looks like that

report.php



<?php


if(isset($_GET["whenithappned"])&&($_GET["howlong"])&&($_GET["alliendescription"])&&($_GET["fangSpotted"])&&($_GET["email"]))
{
 $when_it_happned=$_POST['whenithappned'];
 $how_long=$_POST['howlong'];
 $allien_decription=$_POST['alliendescription'];
 $fang_spotted=$_['fangSpotted'];
 $email=$_POST['email'];
}

 echo "thans for submmiting the form";
 echo "you were obducted" . $when_it_happned;
 echo "and you were gon ". $how_long .'<br>';
 echo 'Describe them: ' . $allien_decription . '<br />';
 echo 'Was Fang there? ' . $fang_spotted . '<br />';
 echo 'Your email address is ' . $email;


?>






allien.php
<!DOCTYPE html>
<html>
<head>
	<title>Alien obducted me...--report an obduction</title>
</head>
<body>
	
	<form method="post" action="report.php" >
		<label for="firstname">First Name:</label>
		<input type="text" id="firstname" name="firstname" /><br />
		<label for="lastname"> Last Name:</label>
		<input type="text" id="lastname" name="lastname" /><br />
		<label for="email">what is your Email addres</label>
		<input type="text" id="email" name="email" /><br />
		<label for="whenithappned">when did it happned</label>,
		<input type="text" id="whenithappned" name="whenithappned" /><br />
		<label for="howlong">how long did you gone</label>
		<input type="text" id="howlong" name="howlong" /><br />
		<label for="howmany">how many did you see</label>
		<input type="text" id="howmany" name="howmany" /><br />
		<label for="alliendescription">describe them</label>
		<input type="text" id="alliendescription" name="alliendescription" size="32" /><br />
		<label for="whatdidthey">what they did for you</label>
		<input type="text" id="whatdidthey" name="whatdidthey" size="32" /><br />
		<label for="fangSpotted">did you see my dog fang</label>
		Yes<input id="fangSpotted" name="fangSpotted" type="radio" value="yes" />
		No<input id="fangSpotted" name="fangSpotted" type="radio" value="no" /><br />
		<img src="fangs.jpg" width="100" height="175" align="my obducted dog fangs" /><br />
		<label for="other">anything else you wanto to add</label>
		<textarea id="other" name="other"></textarea><br />
		<input type="submit" value="report obduction" name="submit" />

	</form>

</body>
</html>



Was This Post Helpful? 0
  • +
  • -

#14 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2836
  • View blog
  • Posts: 11,132
  • Joined: 03-December 12

Re: implement code

Posted 05 May 2019 - 08:04 AM

Donít start a new topic.

Thatís from the head first php book. Whatís the issue?
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6252
  • View blog
  • Posts: 24,024
  • Joined: 23-August 08

Re: implement code

Posted 05 May 2019 - 08:07 AM

if(isset($_GET["whenithappned"])&&($_GET["howlong"])&&($_GET["alliendescription"])&&($_GET["fangSpotted"])&&($_GET["email"]))
{
 $when_it_happned=$_POST['whenithappned'];
 $how_long=$_POST['howlong'];
 $allien_decription=$_POST['alliendescription'];
 $fang_spotted=$_['fangSpotted'];
 $email=$_POST['email'];
}


You're checking whether those variables exist in the $_GET array, then setting your variables from the $_POST array.

Does that logically make sense to you?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2