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
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.
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>
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>
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
Related
how to make the button is inactive after successfully moving the page coba.php and when returning to the page test.php button remains inactive
test.php
`<button type="button" id="Btn" onclick="myFunction()">Klik</button>
<script>
function myFunction()
{var x = document.getElementById("Btn");location.href =
"coba.php";x.disabled = true;}
</script>`
this file
coba.php
<?php echo "Hallo!" ?>
Thank you in advance ^_^
If I understand correctly you want that after the file has been moved successfully you want the button to be disabled
if this is the case, one possible way is to use a variable of type sesssion and check if the page has already been visited as in this example:
test.php:
<?php
session_start();
?>
<html>
<body>
<button onClick="myfunction()"
<?php if(isset(session['load'])){echo("disabled");}?>
>Klik</button>
<script>
function myfunction(){
location.href = "coba.php";}
</script>
<body>
<html>
the coba.php look like this:
<?php
session_start();
$_SESSION['load'] = "true";
?>
<html>
<body>
<?php echo("Hallo!") ?>
<button>Back</button>
<body>
<html>
I am trying to bring up a javascript alert with my variables from php. My upload.php file so far is:
<?php
if(isset($_POST['btn-upload']))
{
$pic = rand(1000,100000)."-".$_FILES['pic']['name'];
$pic_loc = $_FILES['pic']['tmp_name'];
$folder="uploaded_files/";
if(move_uploaded_file($pic_loc,$folder.$pic))
{
?><script>alert('File successfully uploaded.\n![File Upload]('+window.location.href+')');
</script><?php
}
else
{
?><script>alert('Sorry, error while uploading file. Please try again');</script><?php
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Photo Uploader for use in markdown</title>
</head>
<body>
<h2>Photo Uploader & Markdown generator</h2>
<p>Click 'Choose file' to choose the file to be uploaded. The filename should appear. Then click 'upload'. <br>A popup should show you what to copy and paste.</p>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pic" />
<button type="submit" name="btn-upload">upload</button>
</form>
</body>
</html>
I then have my html code which looks like (only the relevant part included):
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pic" />
<button type="submit" name="btn-upload">upload</button>
The purpose of this script is to upload a picture to a server and then display the markdown code for the user to use that image. I am aiming to output the following if the file uploads correctly:
![Alternative Text](http://www.example.com/folder/photo.jpg)
I have tried the following:
<?php
if(isset($_POST['btn-upload']))
{
$pic = rand(1000,100000)."-".$_FILES['pic']['name'];
$pic_loc = $_FILES['pic']['tmp_name'];
$folder="uploaded_files/";
if(move_uploaded_file($pic_loc,$folder.$pic))
{
?><script>alert('File successfully uploaded.\n![File Upload]('+window.location.href+.$folder.'/'.$pic.')');</script><?php
}
else
{
?><script>alert('Sorry, error while uploading file. Please try again');</script><?php
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Photo Uploader for use in markdown</title>
</head>
<body>
<h2>Photo Uploader & Markdown generator</h2>
<p>Click 'Choose file' to choose the file to be uploaded. The filename should appear. Then click 'upload'. <br>A popup should show you what to copy and paste.</p>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pic" />
<button type="submit" name="btn-upload">upload</button>
</form>
</body>
</html>
This results in a working webpage that uploads the file but does not show the js alert.
I have also tried the following:
<?php
if(isset($_POST['btn-upload']))
{
$pic = rand(1000,100000)."-".$_FILES['pic']['name'];
$pic_loc = $_FILES['pic']['tmp_name'];
$folder="uploaded_files/";
<script>var folder = "<?php echo $folder ?>";</script>
<script>var pic = "<?php echo $pic ?>";</script>
if(move_uploaded_file($pic_loc,$folder.$pic))
{
?><script>alert('File successfully uploaded.\n![File Upload]('+window.location.href+folder+'/'+pic+')');</script><?php
}
else
{
?><script>alert('Sorry, error while uploading file. Please try again');</script><?php
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Photo Uploader for use in markdown</title>
</head>
<body>
<h2>Photo Uploader & Markdown generator</h2>
<p>Click 'Choose file' to choose the file to be uploaded. The filename should appear. Then click 'upload'. <br>A popup should show you what to copy and paste.</p>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pic" />
<button type="submit" name="btn-upload">upload</button>
</form>
</body>
</html>
This results in an http error 500
Any advice?
Many thanks,
You can create a function in php and call it where ever you need to call alert.
Function :
function alertMsg($str) {
print("<script>alert('$str')</script>");
}
And call in php as
//string
alertMsg("Success");
//php variable
$alertMsg = "Some alert message";
alertMsg($alertMsg);
//even you can concatenate both
alertMsg("This is an alert. ".$alertMsg);
Hope this helps.
Thanks.
In both attempts you're trying to mix PHP, HTML, and JavaScript as though they were all the same language. They are not. From the perspective of any one of them, code in another one of them is nothing but a string. They can't directly share variables and logic.
See how this line:
alert('File successfully uploaded.\n![File Upload]('+window.location.href+.$folder.'/'.$pic.')');
is attempting to use PHP code (both the variables and the syntax) directly in JavaScript. This is simply resulting in syntax errors in your JavaScript, which your browser's development console is pointing out to you. Instead, enclose the PHP code in <?php ?> tags and echo the result:
alert('File successfully uploaded.\n![File Upload]('+window.location.href+'<?php echo $folder.'/'.$pic ?>'+')');
The second attempt has the same problem, you're putting HTML/JavaScript directly in your PHP:
$folder="uploaded_files/";
<script>var folder = "<?php echo $folder ?>";</script>
This is resulting in PHP syntax errors, which your PHP logs are telling you about (as well as the 500 Internal Server Error you're getting).
PHP code needs to be in <?php ?> tags. Always. So this would be something like:
$folder="uploaded_files/";
?>
<script>var folder = "<?php echo $folder ?>";</script>
<script>var pic = "<?php echo $pic ?>";</script>
<?php
if(move_uploaded_file($pic_loc,$folder.$pic))
Note also that in HTML/JavaScript you don't need a <script> tag for every line of JavaScript code. You can have multiple lines of code in a single <script> element.
Using variable PHP in JS
<?php
if (isset($_POST['btn-upload'])) {
$pic = rand(1000,100000)."-".$_FILES['pic']['name'];
$pic_loc = $_FILES['pic']['tmp_name'];
$folder = "uploaded_files";
if (move_uploaded_file($pic_loc, $folder . '/' . $pic)) { ?>
<script>
alert("File successfully uploaded! " +
"\n" +
location.hostname +
"<?php echo '/' . $folder . '/' . $pic; ?>");
</script>
<?php
} else { ?>
<script>alert("Sorry, error while uploading file. Please try again");</script>
<?php
}
}
?>
location.hostname = $_SERVER['HTTP_HOST'] // localhost
My apologize in advance for my (maybe) stupid question but my knowledge of Javascript/JQuery is almost 0
I have in index.html the following script:
index.html
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#MyDIV")
.load("buttons.php")
});
</script>
<div id="MyDIV"></div>
As you can see, the idea is load buttons.php in a div tag in index.html - That works fine -
buttons.php
<div id="buttom">
<div id="botton5"><img src="5.png" id="5"/></div>
</div>
<script>
$('#botton5').click(function(event){
$("#bottons").load('somepage.php?answer=5'); //here!
});
</script>
Thats also works fine and I receive the information from somepage.php in MyDIV in index.html
but does not work when I include a php in the URL in the line
$("#bottons").load('somepage.php?answer=5&title=<?php echo $title;?>&date=<?php echo $date;?>'); //here!
Including a PHP in load, the div does not load in index.html, can you please support me on how to add a php in the URL in load?
Thanks in advance for your support
#Luis Gerardo Runge In JavaScript you can't use PHP but logically you can take your value in hidden field and use in javascript. i have add some sample code my be it's help you
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<div id="buttom">
<div id="botton5"><img src="5.png" id="5"/></div>
</div>
<div id="bottons">
</div>
<?php
$title = 'your title';
$date = 'yourdate';
?>
<input type="hidden" id="title" name="title" value="<?php echo urlencode($title); ?>">
<input type="hidden" id="date" name="date" value="<?php echo urlencode($date); ?>">
<script>
$('#botton5').click(function(event){
alert('call');
$("#bottons").load('json.php?answer=5&title='+$('#title').val()+'&date='+$('#date').val()+''); //here!
});
</script>
FYI: You need to use urlencode and urldecode for pass parameter through the URL
I have this code, which checks a database and returns some rows to my PHP code containing 4 values (id, playerA, playerB, turn, all INT).
I would like to use that array to build up a table and then append the table to a specific location in the DOM, but i dont know how i could do that.
I could do it another way (get the rows via JS Ajax and then use JS to build and append the table), which i know how, but i dont want to do that.
Is it possible to create a table and append it to a div using php/html/css ?
thanks
<?php
if (isset($_SESSION["userid"])){
$dbManager = DBManager::app();
$manager = new Manager($_SESSION["userid"]);
$gameList = $manager->getGames();
if ($gameList) {
Debug::log("got active games: ".sizeof($gameList);
}
else {
Debug::log("no games");
}
}
else {
Debug::log("no user id");
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css'/>
<script src="jquery-2.1.1.min.js"></script>
<script src='script.js'></script>
<script src='ajax.js'></script>
</head>
<body>
<input type="button" value="createGame" onclick="createGame()">
<divid="gameListDiv">
<div>LOGOUT</div>
</body>
</html>
EDIT
<?php
$table = "";
if ($gameList) {
foreach ($gameList as $game){
$table += "<tr>";
$table += "<td>";
$table += $game["name"];
$table += "</td>";
$table += "</tr>";
}
$table += "</table>";
}
?>
<body>
<input type="form" id="gameName" placeholder="Enter Game Name here"></input>
<input type="button" value="createGame" onclick="createGame()"></input>
<div>
<span>Active Games</span>
<?php echo $table; ?>
</div>
<div>LOGOUT</div>
</body>
You need to understand that the DOM does not yet exist - it is created by the browser, and the browser builds it based on the output of your combined PHP & HTML.
There are many ways to solve this problem without resorting to Ajax calls etc.
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css'/>
<script src="jquery-2.1.1.min.js"></script>
<script src='script.js'></script>
<script src='ajax.js'></script>
</head>
<body>
<input type="button" value="createGame" onclick="createGame()">
<div id="gameListDiv">
<?php
if (isset($_SESSION["userid"])){
$dbManager = DBManager::app();
$manager = new Manager($_SESSION["userid"]);
$gameList = $manager->getGames();
if ($gameList) {
Debug::log("got active games: ".sizeof($gameList);
echo '<table style="width:100%">';
//assuming we can iterate over the $gameList value
foreach($gameList as &$game)
{
//here i assume that the result returned is an object with these properties - it might be the case that you need to do something like $game['playerA'] or $game->getData('playerA') - i am not sure what database lib you are using.
echo '<tr>
<td>'.$game->playerA.'</td>
<td>'.$game->playerB.'</td>
<td>'.$game->turn.'</td>
</tr>';
}
echo '</table>';
}
else {
Debug::log("no games");
}
}
else {
Debug::log("no user id");
}
?>
</div>
<div>LOGOUT</div>
</body>
</html>
In this example we are just running the PHP in-line with the HTML.
You could also do something like this if you wanted to keep all of your database logic at the top of the page and not in-line with the HTML:
<?php
/* Database logic here */
$variable = '<span>this variable could contain any old html that came from the database logic</span>';
?>
<html>
<head>
</head>
<body>
<php echo $variable; ?>
</body>
</html>
If you want to build the list before page load, you can just insert something like this where you want the table to go:
<table>
<?php foreach($gamelist as $game){ ?>
<tr>
<td><?=$game.id></td>
<td><?=$game.playerA></td>
<td><?=$game.playerB></td>
<td><?=$game.turn></td>
</tr>
<? } ?>
</table>
This will work because with PHP, the page hasn't been built and displayed to the user yet when you've got your data in $gamelist. All of your PHP code will be run before the page is actually sent to the user--which means you don't need to "update" the page later, you can just build it now.
If you're looking to collect or update your data after page load (it's unclear from your comments), this is impossible with only PHP as it's a server-side language. You would need to either reload the page or use AJAX for that.
I passed along data via POST to php using JQuery, but when the page direct to the php file, the POST["createVal"] in php doesn't seem to exit after the call back. I showed this in demo below, notice that the $name is undefined when the callback result is clicked. Any idea how to fix this?
I'm trying to make a function that when the returned result was clicked, html page could redirect to the php page in which user input in html file could be printed out.
HTML file
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<input type="text" id="userinput">
<div id="result">
</div>
</body>
<script>
$("input").keyup(function(){
var input=$("input").val();
$.post("process.php",{createVal:input},function(data){
$("#result").html("<a href='process.php'>"+data+"</a>");
})
})
</script>
</html>
php file(process.php)
<?php
if(isset($_POST["createVal"])){
$name=$_POST["createVal"];
echo $name;
}
?>
<?php
echo $name;
?>
change
$("#result").html("<a href='process.php'>"+data+"</a>");
to
$("#result").html("<a href='process.php?createVal="+data+"'>"+data+"</a>");
and
process.php
if(isset($_REQUEST["createVal"])){
$name=$_REQUEST["createVal"];
echo $name;
}
Use this Html Code:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
</head>
<body>
<input type="text" id="userinput">
<div id="result"> </div>
</body>
<script>
$("input").keyup(function(){
var input=$("input").val();
$.ajax({
type: "POST",
url: "http://localhost/stackoverflow/process.php",
data: {'createVal': input},
success: function(data){
$("#result").html("<a href='http://localhost/stackoverflow/process.php?createVal="+data+"'>"+data+"</a>");
}
});
});
</script>
</html>
PHP Code:
<?php
if(!empty($_REQUEST['createVal']) || !empty($_GET['createVal'])){
$name = $_REQUEST['createVal'];
echo $name;
}elseif(!empty($_GET['createVal'])){
$name = $_GET['createVal'];
echo $name;
}
return 1;
?>
I have run and checked this too.
localhost: if you are running this code on localhost
stackoverflow: is the folder name, if you have any folder in localhost for it so replace the name by this.