0

I have problem I use several php codes inside html page and it gave me wrong result like this code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form</title>
</head>

<body>
<form method="post">

<input type="text" name="int1" /> 
+ 
<input type="text" name="int2" />
= 
<?php
if (isset($_POST)) {
    $int1 = $_POST['int1'];
    $int2 = $_POST['int2'];
    echo $int1 + $int2;
}
?>
<br />
<input type="submit" value="Get Sum" />

</form>
</body>
</html>

the right result is calculate the numbers and display it here is no thing display

the other code

<html>
<head></head>
<body>
<ul> 
<?php for($i=1;$i<=5;$i++){ ?>
<li>Menu Item <?php echo $i; ?></li> 
<?php } ?>
</ul> 
</body>
</html>

the right result to display like this

Menu Item 1
Menu Item 2
Menu Item 3
Menu Item 4
Menu Item 5

but when I display the page just display like this

Menu Item

that is meaning the php code didn't work in the page

I don't know what is the solution I want to use php code and php functions inside html page because phonegap.com not accept php page

4

4 Answers 4

1

You cannot run PHP in PhoneGap. PhoneGap loads a local html file in a native webview. You need to have PHP installed to run PHP and this not possible from a mobile device, even if you use native code.

You need to use JavaScript for the typr of processing you are looking for.

Sign up to request clarification or add additional context in comments.

5 Comments

that it will be problem in my project .
if i want to connect to the database in my application what is the best way
You can create a local database on the device using websql or you can create an api to interface with a remote server
I have websql but how to connect without php language
There is nolonger a plugin for this, but the code in this doc is correct. docs.phonegap.com/en/2.9.0/…
0

PHP code is processed only in PHP files ( files with .php extentions). If your file is .html, try to rename it. In your case the PHP not processed and you see anly first li.

Also I recommend to you set error_reporting = E_ALL in php.ini config and restart server. And then you will see what happened with your script

and better use:

<html>
<head></head>
<body>
<ul> 
<?php for ($i=1; $i<=5; $i++) : ?>
<li>Menu Item <?php echo $i; ?></li> 
<?php endfor; ?>
</ul> 
</body>
</html>

4 Comments

Wrong. It depends on server configuration, you can make file with any extension to work like php script, e.g. with AddType application/x-httpd-php .htm .html for Apache
Yes, thanks. But I don't think that autor of question is do it for .html files
And I don't think it's a reason to write nonsense like "PHP code is processed only in PHP files"
@Mesho Try to rename file from i.html to i.php, please? Your PHP interpreter do not process PHP code in this file, it that I wrote in my answer
0

Your first code does not return anything because your isset is not set.

First code:

<?php
if (isset($_POST['int1']) && isset($_POST['int2'])) {
    $int1 = $_POST['int1'];
    $int2 = $_POST['int2'];
    echo $int1 + $int2;
}
?>

Second code:

<html>
<head></head>
<body>
<ul> 
<?php 
for($a = 1; $a <=5; $a++){
echo "<li>Menu Item ".$a."</li>";
}
?>
</ul> 
</body>
</html>

Comments

0

EDIT: After closer inspection it looks as though your code is not being parsed, rename your index.html to index.php

If you view the source you can see the PHP code.


It could be your first section failing..

$_POST is always set but it can be empty

Try this

<?php
if (isset($_POST['int1'], $_POST['int2'])) {
    $int1 = $_POST['int1'];
    $int2 = $_POST['int2'];
    echo $int1 + $int2;
}
?>

isset() can take multiple arguments

1 Comment

It is definitely the fact you are serving a html page and not php. You need to be running on a php enabled server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.