Hey guy, I want to know if there is a way of using PHP in JavaScript. I have no idea how can i get the variables from PHP into JavaScript. I am making a pizza application, the php file is a confirmation page. The user can either go back to change the data or proceed from this page. What the teacher wants the proceed button to do is to put the data into the database. I do not know if there is other way of doing this as am new to PHP and JavaScript. What i am thinking of is that when the user presses "Proceed", the button invokes the insert() method in the JavaScript and that method will insert data into the database. So, in order to do that i need to get the info from the PHP script. Kindly help Thanks

<?
	echo "<div class='topMargin'><center><h1><strong>Order confirmation </strong></h2></center></div>";
	
	$date = date("Y-m-d");
	
	echo '<div class="margin">';
	$cost=0;
	$counter=0;
	echo "<center>";
	echo "Personal data:<br/>";
	
	if (!isset($_POST["name"])){
		echo "Name filed empty";
	}else {
		$name=$_POST["name"];
	}
	
	if (!isset($_POST["phoneNumber"])){
		echo "Enter a phone number";
	}else{
		$phone=$_POST["phoneNumber"];
	}
	
	echo $name."<br/>";
	echo $phone."<br/>";
	echo "**********************<br/>";
	echo "Address:<br/>";
	
	if(!isset($_POST["unitNumber"])){
		echo "Enter Unit number<br/>";
	}else {
		$unitNum=$_POST["unitNumber"];
	}
	
	if(!isset($_POST["streetName"])){
		echo "Enter street name<br/>";
	}else {
		$streetName = $_POST["streetName"];
	}
	
	if(!isset($_POST["city"])){
		echo "Enter city name<br/>";
	}else {
		$city = $_POST["city"];
	}
	
	if(!isset($_POST["zip"])){
		echo "Enter zip code<br/>";
	}else{
		$zipCode = $_POST["zip"];
	}
	
	if (!isset($_POST["province"])){
		echo "Enter province name<br/>";
	}else {
		$province=$_POST["province"];
	}
	
	echo $unitNum." ";
	echo $streetName."<br/>";
	echo $city.", ";
	echo $zipCode." ".$province."<br/>";
	
	if(!isset($_POST["size"])){
		echo "Choose pizza size<br/>";
	}else {
		$pizzaSize = $_POST["size"];
	}
	
	isset($_POST["delivery"]) ? $delivery = $_POST["delivery"] : $delivery = "";
	isset($_POST["box"]) ? $topping = $_POST['box'] : $topping = "";

	echo "**********************<br/>";
	echo "Your order:<br/>";
	echo "Pizza size: ";
	switch($pizzaSize){
		case "Small":
			echo $pizzaSize." ($5)<br/>";
			break;
		case "Medium":
			echo $pizzaSize." ($10)<br/>";
			break;
		case "Large":
			echo $pizzaSize." ($15)<br/>";
			break;
	}
	
	if($topping!=""){
		echo "<br/>Toppings:<br/>";
		foreach($topping as $toppingName){
			$counter++;
			echo "<li>".$toppingName." ($1)<br/>";
		}
		echo "<br/>";
	}
	
	if($pizzaSize=="Small")
	{
		if($delivery=="yes")
		{
			$cost=5+5+$counter;
		}else{
			$cost=5+$counter;
		}
	}else if($pizzaSize=="Medium")
	{
		if($delivery=="yes")
		{
			$cost=10+5+$counter;
		}else{
			$cost=10+$counter;
		}
	}else if ($pizzaSize=="Large")
	{
		if($delivery=="yes")
		{
			$cost=15+5+$counter;
		}else{
			$cost=15+$counter;
		}
	}
	
	if ($delivery!="yes"){
		$delivery=="no";
	}
	if($delivery=="yes"){
		echo "Home delivery ($5 extra)<br/>";
	}else if ($delivery=="no"){
		echo $delivery;
		echo "You chose pick-up<br/>";
	}
	echo "*******************";
	echo "<br/>Total Cost= $".$cost;
	echo "<br/>*******************";
	echo '</div>';
	
	$db = mysql_connect("localhost", "root");
	mysql_select_db("pizza",$db);

	$query = "INSERT INTO table (id, name,number,unit,street,city,zip,province,size,toppings,delivery,date) VALUES ('', 
	'$name','$phone','$unitNum','$streetName','$city','$zipCode','$province','$pizzaSize','$delivery','$date')";
				
	$result = mysql_query($query);
	echo $result;
	if (!$result) {
		$error = "Sorry could not record score";
	}else {
		echo "working";
	}
?>
here is my php code.


P.S. this is in a separate PHP file
Member Avatar for diafol

JS cannot access php unless you use Ajax. Did your teacher ask you to use Ajax? If so, I suggest heading over to jQuery and use their ajax methods. Rolling your own when new to js is a bit of a nightmare.

You can get js to submit to a php file, but there's no point as the action parameter in the form tag will do that. The only benefit from this is client-side validation.

I do not think he asked us to use that. I have no idea how to solve this. Teacher won't tell anything.

If you are asking a question at this level I doubt very seriously that your teacher wants you to delve into ajax calls. PHP is a server side language and javascript is a client side language.

In order to use PHP you have to have a web server with PHP installed. In simple terms, as the page is being retrieved, on the server, the PHP code is being processed. The web browser DOES NOT process PHP.

Javascript is processed by your browser. It is most often used to produce various effects and for form field validation. The web server DOES NOT process javascript.

For this assignment I bet the teacher is not really interested in the javascript but just the PHP code. The easiest way is to have two files. The first one is just an html page to present the form and the second a PHP page to process the request.

Make sure that you order form fields (in the html) are surrounded by the <form> tag. on the <form> tag set the action to the PHP processing page and the method to POST.

<form action="processOrder.php" method="post">

The name attribute on your <input> tags are automatically translated into $_POST variables. For example, if you had the following code:

<label for="fname">First Name</label>
<input name="fname" type="text" />
<input name="submit" type="submit" />

(It is the submit button that sends your form data to the page specified in the <form> tag's action attribute.)

The variable in your PHP page would be $_POST.

Here is a quick example, you can just cut and past these two code segments into their corresponding files.

Save this code as order.html.

<html>
<head>
<title>Order page</title>
</head>
<body>
<form action="process.php" method="post">
<label for="fname">First Name</label>
<input name="fname" type="text" /><br />
<input name="submit" type="submit" />
</form>
</body>
</html>

Save this code as process.php.

<?php
echo $_POST['fname'];
?>

Hopefully you can build your application from here.

yeah i am almost done. Its just the last php confirmation page where i have to submit the data in database at the click of a button. Which i do not know. I am running my php file in xampp.

Great.
You can mix PHP and HTML on the same page so in the process.php page you can just add all the html for the confirmation.

Just remember everything between the <?php ?> tags are interpretted as PHP. Everything outside is treated as html. So after the closing PHP code you can have all the regular html code. If you need to use the PHP $_POST variables in your html, just include them inside the <?php ?> tags.

<p>Thank you <?php echo $_POST['fname'] ?> for your order!</p>

Now you may see some folks use <?= $_POST ?> which is just a shortcut for the previous statement. The only issue is that the shortcut can be turned off in the php.ini file and if this page is every hosted on someone elses server the shortcut might not work.

Just so your instructor doesn't take off points make sure that the HTML you put in your PHP page has all of the required tags (<html>, <head>, <body>). I used to take off points when my students didn't have valid HTML or a <title>.

I have another question. Please help. Am really frustrated right now. My teacher is being stupid wont tell anything. This is what my teacher told me when i asked him for some hints

The order confirmation page is PHP. Have a CONFIRM or CANCEL order button. CANCEL returns to order.html and CONFIRM stores order and returns to home.html.

. I am done the cancel button, my page stores the data into the database as it loads. I know i need to have some restrictions but i cant get my head around it, cause my knowledge is limited. Our teacher did not teach us anything like that and he expects us to do it. Do i need any functions? I have no idea, i do not want to give up on this so please help me.

Thanks.
Appreciate much
Nav.

I have another question. Please help. Am really frustrated right now. My teacher is being stupid wont tell anything. This is what my teacher told me when i asked him for some hints

The order confirmation page is PHP. Have a CONFIRM or CANCEL order button. CANCEL returns to order.html and CONFIRM stores order and returns to home.html.

. I am done the cancel button, my page stores the data into the database as it loads. I know i need to have some restrictions but i cant get my head around it, cause my knowledge is limited. Our teacher did not teach us anything like that and he expects us to do it. Do i need any functions? I have no idea, i do not want to give up on this so please help me.

Thanks.
Appreciate much
Nav.

Ok, this takes a little bit more. You will have to create a PHP page between the order form and the process page. On this page you will need another form but instead of using text inputs use hidden fields and place the data you received into the value of the hidden fields. The Submit button on this confirm.php page will submit the data to the process page. The cancel button can just link to a dead page.

So...
The action on the order form will be confirm.php.
The action on the confirm page will be process.php.

Using the example I previously gave the hidden fields on the confirm page will look like this:

<input name="fname" type="hidden" value="<?php echo $_POST['fname'] ?>" />

The hidden fields does not show the data user entered though. The user should be provided with a summary of the order before submitting the order. The user needs to see the total price of the order.

Member Avatar for diafol

> My teacher is being stupid wont tell anything.

Learning something for yourself is the most effective way of learning. If he does it for you, you're not learning effectively. I don't think that's being stupid. Sorry.

> My teacher is being stupid wont tell anything.

Learning something for yourself is the most effective way of learning. If he does it for you, you're not learning effectively. I don't think that's being stupid. Sorry.

hmm thats true, but supposed to help and give hints. I was frustrated that time.

Member Avatar for diafol

> hmm thats true, but supposed to help and give hints. I was frustrated that time.
Fair one.

:O Thanks ardav and svilla for posting. I got it working am finished it in time :o. It looks so good :'(. Thanks ALOT!!.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.