This question already has answers here:
PHP & mySQL: When exactly to use htmlentities?
(4 answers)
Closed 2 years ago.
I want to do a check if a name already exists in an array.
I have an issue with a name which contains accented chars.
Below is the code is use and when filling in the (french) name Charlène Rodriês and the (german) name Jürgen Günter; it outputs: NOT exists.
How can i catch these names which contain accented characters?
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['bioname'])) {
$bioname = trim(htmlentities($_POST['bioname']));
$array = array('John Doe','Bill Cleve','Charlène Rodriês','мария преснякова','Jürgen Günter');
if (in_array($bioname_raw, $array)) { // if bioname already exists
echo '<div">'.$bioname.' ALREADY exists!</div>';
}
else {
echo '<div">'.$bioname.' NOT exists!</div>';
}
}
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input class="form-control" name="bioname" type="text" placeholder="AUTHORNAME">
<button type="submit" id="cf-submit" name="submit" class="btn btn-primary w-100">POST</button>
</form>
You're comparing apples and oranges.
When you do htmlentities('Charlène Rodriês'), it changes the string and will encode it into: Charlène Rodriês, which obviously won't match Charlène Rodriês in your in_array().
So remove the htmlentities() when you get the value from the $_POST-variable:
$bioname = trim($_POST['bioname']);
and only use that function before you output the data:
echo '<div">'. htmlentities($bioname).' ALREADY exists!</div>';
As a general rule of thumb, don't encode data on input. Only encode data when you use it since different use cases requires different types of encoding.
Related
This question already has answers here:
PHP How to send multiple values within the same name
(2 answers)
I am trying to insert multiple input value with same name
(2 answers)
How to get multiple input values with PHP when name attributes are the same?
(1 answer)
store multiple value with same input name in sql
(1 answer)
PHP Get multiple values from GET with same name
(2 answers)
Closed 4 months ago.
simple php code that works well in terminal:
<?php
$a = array();
for($i=0; $i<3; $i++){
$b = readline('time: ');
$c = readline('money: ');
$d = array('time'=>$b, 'money'=>$c);
array_push($a, $d);
}
print_r($a);
this pushes the values of multiple entries into an array, creating an array of arrays. however, readline() does not work in the browser. i know i can use javascript easily enough but am trying to replicate this simple action using only php and html. and i really like the way readline() works. i have tried variations on the following but am left scratching my head:
<form method="POST">
<?php
for($i=0; $i<3; $i++){
?>
<input name = 'time'>
<input name = 'money'>
<?php
}
?>
<input type="submit">
</form>
<?php
print_r($_POST['time']);
was hoping print_r($_POST['input name']) would return an array, but instead only gives the last input entry. is there a straight forward way to do this with php, or do i HAVE to use a client-side script like javascript?
Just add [] to input names:
<form method="POST">
<?php
for($i=0; $i<3; $i++){
?>
<input name = 'time[]'>
<input name = 'money[]'>
<?php
}
?>
<input type="submit">
</form>
<?php
print_r($_POST['time']);
This question already has answers here:
How to get PHP $_GET array?
(8 answers)
Closed 2 years ago.
I got a challenge in which I have this code in a PHP file:
<?php
include('secretfile.php');
if(isset($_GET['a']) && isset($_GET['b']))
{
$a = $_GET['a'];
$b = $_GET['b'];
if (!empty($a) && !empty($b))
{
if($a===$b)
{
if(isset($_GET['a']) && isset($_GET['b']))
{
$a = $_GET['a'];
$b = $_GET['b'];
if($a!==$b)
{
echo $secretcode;
}
}
}
}
}
I have to print secretcode in webpage with OUT changing the PHP file.
How can I do it?
I tried by giving parameters through URL like this:
http://127.0.0.1:8080/?a=1&b=1&a=22&b=33
But it didn't work. The file is taking the last values directly and no matter what, I couldn't go past the 13th line. I went through a lot of answers but I go no solution.
Is it possible to do it? If yes, how?
To pass multiple values for a or b, you need to use the [] notation like below:
http://127.0.0.1:8080/?a[]=1&b[]=1&a[]=22&b[]=33
That being said, the PHP code in your file seems to be poorly written.
To pass an array on html form you can use [] like
<form method="GET">
<input type="text" name="a[]" />
<input type="text" name="b[]" />
<form/>
This question already has answers here:
What does "1" mean at the end of a php print_r statement?
(3 answers)
Closed 5 years ago.
I have written this code to take the sting from the textbox and want to replace any badwords with "***" but I don't know why I am getting one extra "1" at the end..you can have a look of output in the picture.
here is my code
<!DOCTYPE>
<html>
<head>
<title> Mind Your Language </title>
</head>
<body>
<form action="" method="post">
<input type="text" name="string">
<input type="submit" name="submit" value="Filter">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
$badWords = ['badword1', 'badword2', 'badword3', 'badword4', 'badword5'];
$string = $_POST['string'];
foreach($badWords as $badWord) {
$string1 = str_replace($badWord, "***", $string);
}
echo print_r($string1);
}
?>
print_r() displays information about a variable in a way that's readable by humans.
If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.
When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.
Either you need to use echo print_r($string1, true); or simply print_r($string1);
This question already has answers here:
Generate Array from a comma-separated list - PHP [duplicate]
(3 answers)
Closed 8 years ago.
I have a form that asks for comma separated phone numbers, for example, if user enters following in "Phone" field:
9999999999,8888800000,7777788888
Then want to store them as an array just like:
$contacts = array ("9999999999","8888800000","7777788888");
How can I do that?
I tried:
$contacts = array();
if (is_array(#$_POST['phone']))
{
foreach($_POST['phone'] as $one)
{
$contacts[] = basename($one);
}
}
$myArray = explode(',', '9999999999,8888800000,7777788888');
You will have to fetch the form input with the global variable called $_POST, $_POST listens to the name of the html element.
Let's say we got
<form method="POST">
<input type="text" name="phoneNumber1" />
<input type="text" name="phoneNumber2" />
<input type="text" name="phoneNumber3" />
</form>
Then we are able to fetch the data in it like this.
$contacts = array($_POST['phoneNumber1'], $_POST['phoneNumber2'], $_POST['phoneNumber3']);
This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 4 months ago.
If I have a form:
<form action="/page.php" method="POST">
<input name="length[<?=$ID;?>]['00:12:00']">
</form>
So, on the BACK end, I clearly have an array, but I need to reference the ID number above, i.e.
foreach($_POST['length'] AS $p) {
echo($p['ID']);
echo($p['ID']['length_number']);
}
Is there a way to structure the front end form input data differently so it is easier to comb through on the back end?
You could reference it by using a hidden input field to grab the ID.
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Try using something like this:
foreach($_POST['length'] AS $id=>$values) {
echo $id;
print_r($values);
}
The the above $values will be an array such as array('00:12:00'=>'{value of the input field}')