php not sense to special characters - php

<?php
mb_detect_order('UTF-8,eucjp-win,sjis-win');
mb_internal_encoding('UTF-8');
echo mb_internal_encoding();
function convert($a) str_replace('â','a',$string);
$e=$_POST['aaa'];
?>
<!doctype html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" >
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
<form method="post" action="#">
<textarea id="aaa" name="bbb" placeholder="send" ></textarea>
**mb_internal_encoding(); output: "UTF-8"
textarea input: "âb" || php-output:âb script not convert "â"=>"a"
file is without BOM**
these may be other reasons?? what else can I try?

str_replice('â','a',$string);
should be:
str_replace('â','a',$string);

some typos there :
function convert($a) str_replice('â','a',$string);
should be :
function convert($a){return str_replace('â','a',$a);}
not replice(),$string should be $a , also if you want to see the output from the function you should return it.
so when you call:
<?php echo convert("â");?>
it would output just fine

Related

Report generator - save and redirect to report

I am creating a report generator, that should save a report and also redirect the user to a version of the report on a specific url (in this case I just added yahoo as test).
Question: Is it possible to save a file and redirect a user to an url in same script?
Problem: The script does the redirect but does not store the file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Report generator</title>
</head>
<body>
<pre>
<form method="post" action="https://se.yahoo.com" target="_blank">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<input type="submit" name="button_1" value="button_1">
</form>
<?php
var_dump($_POST);
// Button event listener.
if (isset($_POST["button_1"])) {
$json_data = "string";
$data = json_encode($json_data);
file_put_contents(
"user_data/data.txt",
$json_data
);
}
?>
</body>
</html>
If you put https://se.yahoo.com is the action attribute that is where the form will post the form data to, so it wont go to this script at all
<?php
// Button event listener.
if (isset($_POST["button_1"])) {
$json_data = "string";
$data = json_encode([$json_data]);
file_put_contents("user_data/data.txt",$json_data);
// really not sure if this is what you wanted to do, bit of a guess really
header('Location: https://se.yahoo.com?fname='.$_POST['fname']);
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Report generator</title>
</head>
<body>
<form method="post" action="" target="_blank">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<input type="submit" name="button_1" value="button_1">
</form>
</body>
</html>

how to store a value of an HTML input in a cookie in PHP

here is the html code
<!
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="test.php" method="POST">
<input type="text" name="pseudo">
<input type="submit">
</form>
</body>
</html>
the form returns the value of the input in a page called test.php and the stock in a coockie but it does not work
here is the php code
<?php
setcookie("pseudo", $_POST['pseudo'], time() + 365*24*3600);
echo "Your name is " . $_COOKIE['pseudo'];
you are missing the cookie name parameter which is required to set a cookie.
please see the below implementation
setcookie("Cookie_Name", $_POST['pseudo'], time() + 365*24*3600);

Get html value to pass to php withini the file

How can I get an HTML value to send to PHP. I would like to avoid using a form. For example I have an input:
<input name="input" id="input">Input</input>
while in PHP:
$input = $_POST['input'] --> but didn't work
or
$input = $_GET['input'] --> still didn't work
I know that I will be able to get it using form then action="another file" but I want it within a file. Please help. Thank you.
If you simply want to use js to append the input value in url variable then you can use js function as follows;
client.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="txt">
<a onclick="append()" id="anch" href="server.php?text">Send value</a>
</body>
</html>
<script>
function append()
{
txt=document.getElementById('txt');
anch=document.getElementById('anch');
anch.href=anch.href+"="+txt.value;
}
</script>
server.php
$variable = $_GET["input"];
echo $variable;
Output on localhost:
After clicking send value:-
Update: I just read the end of your question. If you want to show the value of input on same page/file then you can use following code;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="txt">
<?php
if(isset($_GET['text']))
{
$text=$_GET['text'];
echo "<a onclick='append()' id='anch' href='?text=$text'>Enter Value</a>";
}
else
{
echo "<a onclick='append()' id='anch' href='?text'>Enter Value</a>";
}
?>
<br>
<?php
if(isset($_GET['text']))
echo "<div id='values'>".$_GET['text']."<div>";
?>
</body>
</html>
<script>
function append()
{
txt=document.getElementById('txt');
anch=document.getElementById('anch');
if(anch.href.match(/=/)=="=")
anch.href=anch.href+txt.value+"<br>";
else
anch.href=anch.href+"="+txt.value+"<br>";
}
</script>
Output:
Check output on phpFiddle
It seems that the way to go is to use the GET method.
You will need to access your page with the get parameters included like this:
http://localhost/index.php?input=VALUE_HERE
And in your php file:
$variable = $_GET["input"];
echo $variable;
But if you insist to use inline html elements to get data, you need to use javascript for that like this for jquery:
alert("This is the value of the input" + $("#input").val());
Don't use this
<input name="input" id="input">Input</input>
Use this
<input name="input" id="input">

Can't populate form input using php and url

I'm having some issues populating my input html tag with the parameter in the url.
Here's the Url I'm using:
www.website.com/index.html?inventory=100
And here's the basic html going on
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
</head>
<body>
<input type="text" name="inventory" value="<?php echo 00((isset($_GET["inventory"]))?htmlspecialchars($_GET["inventory"]):""); ?>"/>
</body>
</html>
Help!

Auto-fill form with link to external page

I am looking for a way for a text field and submission button to take me to a new site, fill out a text input field and hit enter. The page I want to link to is Googles speed test. I know I can link to spped test results as well like this:
https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2FYOURDOMAINHERE&mobile=false
but how can I have a customer fill out a "test my page" field on my site, hit submit, and it create a link to:
https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2FYOURDOMAINHERE&mobile=false
with their field submission in the "YOURDOMAINHERE" area of the link. This seems linke not a huge task but i cannot wrap my head around it, php, javascript??? not sure. Any help would be greatly appreciated.
You can do something similar to this, although if Google catches you according to their TOC they *could* lock or ban your account:
<?php
if (!empty($_POST['url'])){
$url = preg_replace('!http[s]://!','',strip_tags($_POST['url']));
$url = preg_replace('![%]!','_',urlencode($url));
$newlink = "https://developers.google.com/speed/pagespeed/insights#url=$url&mobile=false";
$page = '<!doctype html>
<html lang="en">
<head>';
$page .= '<script type="text/javascript">
function replaceDoc()
{
window.location.replace("'.$newlink.'")
}
</script>';
$page .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page for testing</title>
</head>
<body onload="replaceDoc()">
Test your page.
</body>
</html>
';
} else {
$page = '<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Checker</title>
</head>
<body>
<form method="post">
Enter your URL:<br />
<input name="url" type="text" style="width:50em;" />
<input type="submit" name="submit" value="Check your page" />
</form>
</body>
</html>';
}
echo $page;
?>
Be careful directly injecting headers:
<?php
if (!empty($_POST['url'])){
$url = preg_replace('!http[s]://!','',strip_tags($_POST['url']));
$url = preg_replace('![%]!','_',urlencode($url));
$newlink = "https://developers.google.com/speed/pagespeed/insights#url=$url&mobile=false";
header("Content-Type: application/x-www-form-urlencoded");
header("Referer: https://developers.google.com/speed/pagespeed/insights");
header("Location: $newlink");
} else {
$page = '<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Checker</title>
</head>
<body>
<form method="post">
Enter your URL:<br />
<input name="url" type="text" style="width:50em;" />
<input type="submit" name="submit" value="Check your page" />
</form>
</body>
</html>';
}
echo $page;
?>
make a submit.php
have the form submit to that script with the "DOMAIN NAME"
your php should look something like this..
$domainName = $_POST["domainname"];
$redirectURL = "https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2F".$domainName."&mobile=false";
header('Location: $redirectURL');
That script should work havn't tested it but it'll give you an idea...
make sure you sanitize your inputs too... :) good luck
Hows about a simple str_replace? Something like:
<?php
$link = "https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2FYOURDOMAINHERE&mobile=false";
$replace = "YOURDOMAINHERE";
if(isset($_POST['myDomain'])){
$newHeader = str_replace($replace, $_POST['myDomain'], $link);
header("Location: ".$newHeader);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>googleDomain</title>
</head>
<body>
<form action="domain.php" method="post">
<input type="text" name="myDomain" />
<input type="submit" />
</form>
</body>
</html>
I haven't tested this at all, but should be something pretty similar

Categories