Error at converting PHP string in HTML code - php

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;
?>

Related

HTML / PHP / SQL How to display a button under certain circumstances?

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>

Using html in php

I have a problem with this statement:
after this i just get blank page.
so the whole code looks like this :
im tyrying to use html in php and than php again in html:
the whole rest works and if i replace '' with add2.php it works but it writes something before i even pick something
<?php
function login()
{
echo "?";
}
$get = $_GET['Login'];
$get = $_POST['Login'];
echo $get;
var_dump($get);
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<div class="content">
<?php
require 'connection.php';
include 'user_verify.php';
include 'access_verify.php';
mysql_select_db("idoctor_db") or die("Bląd podczas wybierania bazy danych");
$select = 'SELECT * FROM users;';
$query = mysql_query($select);
// Ustaw domyślny element; tutaj są ustawione kreseczki, żeby nic nie sugerować ;P
echo '<form action="'<?php login(); ?>'" method="post">
Jezyk <select name="Login"><option value="0">------------------</option>';
while ($language = mysql_fetch_object($query))
{
echo '<option value="'.$language->Login.'" selected>'.$language->Login.'</option>';
}
echo '</form>';
?>
<input type="submit" value="Login" name="submit"/>
</div>
</body>
</html>
The HTML form element doesn't render anything on screen... add some content and you will see it is working.
<?php
function login()
{
echo "?";
}
?>
<?php
echo '<form action="<?php login(); ?>" method="post">';
echo 'Hello World!';
echo '</form>';
?>
Make sure your quotes are properly closed too. (I'm not sure if the missing closing single quote in your sample was a copy/paste error or not)
<?php
echo '<form action="<?php login(); ?>" method="post">
?>
Is a syntax error, you forgot the closing single quote and semicolon.
That said, I do not think this code will do what you expect: By using echo you will send "<?php login(); ?>" to the browser, not run it in the PHP interpreter.
This:
<?php
echo '<form action="<?php login(); ?>" method="post">
?>
Should be:
<?php
echo "<form action='" . login() . "' method='post'>";
?>
But then your login function needs to be fixed because printing out ? is not going to work.
function login()
{
return "login.php";
}

sending href with parameter in a loop

<!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']

php str_replace returns empty string

<?php
$html = '<form id="form1" name="f1" action="/jk" dummy';
$html = str_replace(
'<form id="form1" name="f1"',
'<form id="form1" name="f1" xxxxxxxxxxxxx',
$html);
echo $html;
Why the result is empty?
I'm using PHP 5.3.27 (cli)
Thanks.
$html = '<form id="form1" name="f1" action="/jk" dummy';
$html = str_replace(
'<form id="form1" name="f1"',
'<form id="form1" name="f1" xxxxxxxxxxxxx',
$html);
echo highlight_string($html, true);
another way to show is:
header("Content-Type: text/plain");
echo $html;
Try this method,
<?php
echo 'hello<br>';
$html = "abcde<br>";
echo $html;
$a = substr_replace($html,
'xyzhi',
0);
echo $a;
?>
Str_replace will need word which you want to replace from the string.I hope this may work.
You can look at source code ctr+u you will find it is replaced successfully there, but Why cannot see it on the browser page? because the browser thinks of it as a tag.
So to show it on the browser page we can use htmlentities():
$html = '<form id="form1" name="f1" action="/jk" dummy';
$html = str_replace(
'<form id="form1" name="f1"',
'<form id="form1" name="f1" xxxxxxxxxxxxx',
$html);
echo htmlentities($html);
Output:
<form id="form1" name="f1" xxxxxxxxxxxxx action="/jk" dummy

php form action path with variable

I'm really thinking I must have a badly formed php echo statement at the beginning of the but Dreamweaver is telling me that I have no syntax errors. My process.php is never getting called.
$file = dirname(__FILE__) . '/customBook-index.php';
$plugin_path = plugin_dir_path($file);
$plugin_url = plugin_dir_url($file);
<?php
echo '<form method="post" action="'.$plugin_url.'process.php" />';
echo'<select name="clients">';
foreach($clientsArray as $client){
echo'<option value="'.$client.'">'.$client.'</option>';
}
echo'</select>';
echo '</form>';
?>
You don't have to echo all of your HTML. You could also write:
<?php
$file = dirname(__FILE__) . '/customBook-index.php';
$plugin_path = plugin_dir_path($file);
$plugin_url = plugin_dir_url($file);
?>
<form method="post" action="<?=$plugin_url?>process.php" />
<select name="clients">
<?php
foreach($clientsArray as $client){
?>
<option value="<?=$client?>"><?=$client?></option>
<?php
}
?>
</select>
</form>
Maybe that's easier for you to read and understand!? What is the output of $plugin_url.'process.php in your HTML? I think that either the path does not match or that you don't submit your form correctly.

Categories