php str_replace returns empty string - php

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

Related

Error at converting PHP string in HTML code

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

how to echo input text in For{}

I write below code and put an html input text in for :
for($i=0;$i<5;$i++)
echo '<input type="text" name="subject">';
but when i want to echo the that value I type in to the above text box
it returns "" nothing
echo $_POST['subject'];
I have
<form name="form1" method="post" action="index.php">
Tag top of this codes ?
Sorry if I had Mistake
Make the input as array and read the values in PHP using loop like below.
HTML code :
<form name='myForm' action='index.php' method="post">
<?php
for {$i=i;$i<5;$i++} {
echo "<input type='text' name='subject[]'>";
}
?>
</form>
PHP Code :
$subjectArray = $_POST['subject'];
foreach($subjectArray as $key => $val) {
if($val != "") {
echo $key."==".$val."<br>";
}
}
It's bad manner to echo your HTML code. It can become annoyingly hard for someone to read the code after you (for exemple, in a company)
I would suggest doing this:
for($i=0;$i<5;$i++) {
?><input type="text" name="subject"><?php
}
If you want to input back in the form what was just typed, then you're doing too much.
Is your form in index.php too?! If so, action="index.php" become action=""
• With your array:
$subjectArray= array();
// Gather data from $_POST if it's the subject form
if(!empty($_POST) aa isset $_POST['subjectForm']) {
// Remove the hidden input from the list
unset($_POST['subjectForm']);
// Add the remaining entry to $subjectArray
foreach($_POST as $key => $value) {
$subjectArray[$key]= $value;
}
}
<form name="myForm" action="" method="post">
<!-- hidden input to find our form after validation -->
<input type="hidden" name="subjectForm" value=1>
<?php
for($i=1; $i<=5; $i++) {
?><input type="text" name="subject<?= $i ?>" value="<?php if(isset(subjectArray['subject'.$i])) { echo $_POST['subjectArray'.$i]; } ?>"><?php
}
?>
</form>
• Without your array:
<form name="myForm" action="" method="post">
<?php
for($i=1; $i<=5; $i++) {
?><input type="text" name="subject<?= $i ?>" value="<?php if(isset($_POST['subject'.$i])) { echo $_POST['subjectArray'.$i]; } ?>"><?php
}
?>
</form>

$_POST and code manipulation

I want to grab the value of a field using $_POST, manipulate it, then pass the value back to the same page to the same field before the PHP code manipulates it.
If I put the PHP code after the field, it manipulates the code, reloads the page but doesn't put the manipulated code back into the field.
if (!isset($input)) {
$input = '';
}
echo '<form id="testform" method="post" action="">';
echo '<input type="text" name="inputText" value="' . $input . '">';
echo '<button type="submit" name="button"> Button </button>';
echo '</form>';
$input = $_POST['inputText'];
if(isset($_POST['inputText'])) {
$input = $input . ' manipulated';
}
echo $input; //test
If I put the PHP code before the field, it can't find the field to manipulate the value...
if (!isset($input)) {
$input = '';
}
$input = $_POST['inputText'];
if(isset($_POST['inputText'])) {
$input = $input . ' manipulated';
}
echo $input; //test
echo '<form id="testform" method="post" action="">';
echo '<input type="text" name="inputText" value="' . $input . '">';
echo '<button type="submit" name="button"> Button </button>';
echo '</form>';
Obviously the first approach is more correct, but how do I pass the $input variable to the field before the rest of my PHP manipulation code executes?
I tried $_POST['inputText'] = $input as a desperate attempt but nothing..
Well, from what I've understood in your explanation, you want to change the input value to something else and show it in he same field. If that's correct, you may want to do this:
<form id="testform" method="post" action="">
<input type="text" name="inputText" value="<?php echo ( isset($_POST['inputText']) ) ? sprintf( '%s manipulated', $_POST['inputText'] ) : ''; ?>">
<button type="submit"> Send </button>
</form>
Let me know if that's what you wanted. Regards !
Try
$input = isset($_POST['inputText']) ?$_POST['inputText'] :'';
in the begining instead of
if (!isset($input)) {
$input = '';
}

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";
}

How can I schedule the submit of form depending upon a variable?

<form action="<?php if($count==3)
echo"populate3.php";
else
echo"populate1.php"; ?>" method="get" id="form1">
Is this correct to write ?
What could be alternatives ?
<?php
$action="":
if($count==3)
{
$action="populate3.php";
}
else
{
$action="populate1.php";
}
echo ("<form action=\"$action\" method=\"get\" id=\"form1\">");

Categories