I have this PHP code:
$htmlCode = '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
</form>
';
And I want to put it in a part of my HTML using this:
<html>
<?php
echo $htmlCode;
?>
</html>
But when I open the file it shows this:
And I don't know why. Please help
Your PHP-Code should look like:
<?php
$htmlCode = '
<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">
</form>
';
?>
this is big mistake
try this code
<?php
$path = htmlspecialchars($_SERVER["PHP_SELF"]);
$htmlCode = "<form action='{$path}' method='post'></form>";
echo $htmlCode;
even you don't need $path
this is also work
<?php
$htmlCode = '<form action"" method="post"></form>';
echo $htmlCode;
?>
So I have a script using HTML, PHP, and mysql, and I want to display a button under certain circumstances.
Here is my script:
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$count = mysqli_num_rows($query);
if($count != "1"){
$output = '<h2>No result found!</h2>';
}else{
while($row = mysqli_fetch_array($query)){
$s = $row['name'];
$output .= '<h2>Found: '.$s.'</h2><br>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php echo $output; ?>
</body>
</html>
Specifically, I want to display the return button only when the output is "No results found", when the amount of rows in the SQL database matching the given query is not 1. How could I go about accomplishing this? I'm relatively new to PHP and mySQLi, but from my research I couldn't figure out how to do such a task, any ideas?
<?php
if ($count==0) {
echo '<input type="submit" name="return" value="Return">';
}
?>
If you want a much cleaner html code, do this:
<form method="POST" action="index.html">
<?php if ($count!= "1") : ?>
<input type="submit" name="return" value="Return">
<?php else : ?>
<!-- put your other button here -->
<?php endif; ?>
</form>
You can read more about escaping from HTML here.
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$results = mysqli_fetch_array($query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php if(0 < count($results)) ?>
<?php foreach($results AS $row) : ?>
<h2><?= $row['name'] ?></h2>
<?php endforeach; ?>
<?php else : ?>
<H2> No results found!</h2>
<?php endif; ?>
</body>
</html>
Im new to php and tryed to output one of the input (radio) values.When i used $_POST to output i got Undefined index error/notice and when i used var_dump on $_POST i got array(0) { }.How could i output values / what might be the problem ?
require('connect.php');
?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<?php
$kusimus = $db->prepare('SELECT k.kusimus FROM kusimus AS k JOIN kusimus_valik AS kv ON k.Kusimus_id = kv.Kusimus_id WHERE k.Is_active = 2');
$vastused = $db->prepare('SELECT kv.valik FROM kusimus AS k JOIN kusimus_valik AS kv ON k.Kusimus_id = kv.Kusimus_id WHERE k.Is_active = 2');
$kusimus->execute();
$vastused->execute();
$result2 = $vastused->fetchAll();
$result = $kusimus->fetchAll();
?>
<?php echo '<b>'.$result[1][0].'</b><br>'; ?>
<?php if(!isset($result2[0][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[0][0].'">'.$result2[0][0].'<br>';
} ?>
<?php if(!isset($result2[1][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[1][0].'">'.$result2[1][0].'<br>';
} ?>
<?php if(!isset($result2[1][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[2][0].'">'.$result2[2][0].'<br>';
} ?>
<?php if(!isset($result2[3][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[3][0].'">'.$result2[3][0].'<br>';
} ?>
<?php if(!isset($result2[4][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[4][0].'">'.$result2[4][0].'<br>';
} ?>
<br><br><br><br><br><br><br>
<input type="submit" value="Vasta">
</form>
<?php
var_dump($_POST);
?>
</body>
</html>
<?php
$db = null;
?>
You should use a switch statement instead of all of those if and else statements. You have to specify what variable you want to use var_dump on. $_POST isn't a variable. $_POST['var'] would be an example of a variable.
I seriously hope this code isn't going to be deployed.
Also, if you want more people to take you seriously, focus on improving your spelling, grammar, and punctuation before you focus on programming.
I have a form on one page linking to a PHP file (action), now the PHP result is being displayed in this PHP file/page. But I want the result to be displayed on the page with the form. I have searched thoroughly and couldn't find it anywhere. Perhaps any of you can help?
Code: /citizens.php (main page)
<form method="post" action="/infoct.php">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="submit">
</form>
Code: /infoct.php
<!DOCTYPE html>
<html>
<head>
<!-- <meta http-equiv="refresh" content="0; url=/citizens.php" /> -->
</head>
<body>
<?php {
$ID2 = isset($_POST['ID']) ? $_POST['ID'] : false;
}
$connect = mysql_connect('localhost', 'root', 'passwd');
mysql_select_db ('inhabitants');
$sql = "SELECT `Name`, `Surname`, `DOB`, `RPS`, `Address` FROM `citizens` WHERE ID = $ID2";
$res = mysql_query($sql);
echo "<P1><b>Citizen Identification number is</b> $ID2 </p1>";
while($row = mysql_fetch_array($res))
{
echo "<br><p1><b>First Name: </b></b>", $row['Name'], "</p1>";
echo "<br><p1><b>Surname: </b></b></b>", $row['Surname'], "</p1>";
echo "<br><p1><b>Date of birth: </b></b></b></b>", $row['DOB'], "</p1>";
echo "<br><p1><b>Address: </b></b></b></b></b>", $row['Address'], "</p1>";
echo "<br><p1><b>Background information: </b><br>", $row['RPS'], "</p1>";
}
mysql_close ($connect);
?>
</body>
</html>
My fixed code thanks to Marc B
<form method="post">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="submit">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$ID = isset($_POST['ID']) ? $_POST['ID'] : false;
$connect = mysql_connect('fdb13.biz.nf:3306', '1858208_inhabit', '12345demien12345');
mysql_select_db ('1858208_inhabit');
$sql = "SELECT `Name`, `Surname`, `DOB`, `RPS`, `Address` FROM `citizens` WHERE ID = $ID";
$res = mysql_query($sql);
if ($ID > 0) {
echo "<p><b>Citizen Identification number is</b> </p>";
while($row = mysql_fetch_array($res))
echo "<br><p><b>Surname: </b></b></b>", $row['Surname'], "</p>";
echo "<br><p><b>First Name: </b></b>", $row['Name'], "</p>";
echo "<br><p><b>Date of birth: </b></b></b></b>", $row['DOB'], "</p>";
echo "<br><p><b>Address: </b></b></b></b></b>", $row['Address'], "</p>";
echo "<br><p><b>Background information: </b><br>", $row['RPS'], "</p>";
mysql_close ($connect);
}
else {
echo "<p>Enter a citizen ID above</p>";
}
}
?>
DB Snap
A single-page form+submit handler is pretty basic:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
... form was submitted, process it ...
... display results ...
... whatever else ...
}
?>
<html>
<body>
<form method="post"> ... </form>
</body>
</html>
That's really all there is.
Use code on the same page (citizens.php)
<?php
if (isset($_POST)) {
Do manipulation
}
?>
Else use ajax and remove action method from form.
<form method="post" id="contactForm">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="buttom" id="submitId">
</form>
<script>
$("#submitId").click(function(){
var Serialized = $("#contactForm").serialize();
$.ajax({
type: "POST",
url: "infoct.php",
data: Serialized,
success: function(data) {
//var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this
// do what ever you want with the server response
},
error: function(){
alert('error handing here');
}
});
});
</script>
And in your infact.php in the end Echo the data so that ajax will have the data in return.
You could just put everything in infoct.php, like this:
<!DOCTYPE html>
<html>
<head>
<!-- <meta http-equiv="refresh" content="0; url=/infoct.php" /> -->
</head>
<body>
<form method="post" action="/infoct.php">
<input type="text" name="ID" placeholder="ID" value="<?php isset($_POST['ID']) ? $_POST['ID'] : '' ?>">
<input name="set" type="submit">
</form>
<?php
if (isset($_POST['ID'])) {
$ID2 = $_POST['ID']; // DO NOT FORGET ABOUT STRING SANITIZATION
$connect = mysql_connect('localhost', 'root', 'usbw');
mysql_select_db ('inhabitants');
$sql = "SELECT `Name`, `Surname`, `DOB`, `RPS`, `Address` FROM `citizens` WHERE ID = $ID2";
$res = mysql_query($sql);
echo "<P1><b>Citizen Identification number is</b> $ID2 </p1>";
while($row = mysql_fetch_array($res))
{
echo "<br><p1><b>First Name: </b></b>", $row['Name'], "</p1>";
echo "<br><p1><b>Surname: </b></b></b>", $row['Surname'], "</p1>";
echo "<br><p1><b>Date of birth: </b></b></b></b>", $row['DOB'], "</p1>";
echo "<br><p1><b>Address: </b></b></b></b></b>", $row['Address'], "</p1>";
echo "<br><p1><b>Background information: </b><br>", $row['RPS'], "</p1>";
}
mysql_close ($connect);
}
?>
</body>
</html>
Do not forget about string sanitization !
I have found the solutions to the folowing problems:
Display results on same page
Thanks to Marc B
A single-page form+submit handler is pretty basic:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
... form was submitted, process it ...
... display results ...
... whatever else ...
}
?>
<html>
<body>
<form method="post"> ... </form>
</body>
</html>
That's really all there is.
Only first value is showing
I resolved this problem by adding this to my code:
while($row = mysql_fetch_array($res)) {
$surname=$row['Surname'];
$name=$row['Name'];
$dob=$row['DOB'];
$address=$row['Address'];
$RPS=$row['RPS'];
Now all the values are being displayed instead of only the first one.
Display results on same page
Well I've stumbled upon this with the same problem
and I found out you can simply require the other file.
include_once("PATH_TO_FILE")'.
in /citizens.php
<?php include_once="infoct.php" ?>
<form> ... </form>
<div>
<?php $yourdata ?>
</div>
$yourdata should be html.
Do not forget about string sanitization !
Make sure to remove action from the form
Better than having all logic and Html in one file.
<!doctype html>
<html>
<head>
<title>Main Page</title>
</head>
<?php
session_start();
?>
<form action="new_question.php" method="post">
<input type="hidden" name="sid" value="<?php echo $_SESSION['username']?>">
<input type="submit" value="New Question">
</form>
<?php
include ("connection.php");
$result = mysqli_query($con,"SELECT * FROM question_table");
while($row = mysqli_fetch_array($result))
{
echo "" . $row['question'] . $row['q_id'] . "";
echo "<br>";
}
?>
<body>
</body>
</html>
I have 5 question in my database each with a id. this page prints them as a link in loop. upon clicking any of the link it goes to "question.php" file. there i want to echo the question from the database that was clicked previously. the problem is in "question.php" file how do i find out which link was clicked among thus 5. should i send a parameter along with the link? how the parameter will change in each loop? how do i do it in this page? if i do send a parameter with the link how do i receive it in the "question.php" file?
Echo the id as a parameter on the anchor. We can also remove the id from the anchor text since it's not needed there anymore.
while($row = mysqli_fetch_array($result))
{
echo '' . $row['question'] . '<br>';
}
And then in question.php do $_GET['id']