Using $_GET funcion instead of using plain text - php

I have a file on my server called "form2.php", this is what it looks like:
<form action="strlen2.php" method="get">
<input type="text" name="text13"/>
<input type="submit" value="Submit!"/>
</form>
I have it transfer to another file called "strlen2.php", this is what it looks like:
<?php
$text="$_GET["text13"]";
$more="Too much! Take away some text.";
$equal="Great! You entered the right amount.";
$less="Not enough! Enter some more text.";
if(strlen($text)>3)
{
echo $more;
}
elseif(strlen($text)==3)
{
echo $equal;
}
else
{
echo $less;
}
?>
What can I change on line 3 that makes it execute the $_GET function instead of looking at it as plain text?

$_GET is a variable. Remove the quotes around it.
$text = $_GET['text13'];

In PHP, the predefined $_GET variable is used to collect values in a form with method="get".
<form action="strlen2.php" method="get">
and if you do print_r($_GET); in your strlen2.php, you'll be able to see the result.

Remove the quotes. $_GET is not a function, but an array.
$text = $_GET["text13"];

If the value of $_GET['text13'] DOES happen to contain a reference to a function, you would need to do something like this:
$text = $_GET['text13'];
$text();

$_GET is not a function ;) it's a variable. Remove the quotes and it should work like a charm :)
eg :
$text=$_GET["text13"];
$more="Too much! Take away some text.";
$equal="Great! You entered the right amount.";
$less="Not enough! Enter some more text.";

<?php
$text = $_GET["text13"];
....
Don't put quotes around the assignment!

This would never work:
$text="$_GET["text13"]";
Use this instead:
$text = $_GET['text13'];

Try this:
$text= $_GET["text13"];

Related

Alternative to $_GET["param"] to keep %0A line breaks?

I am sending info from an HTML form through the URL to be used at the destination web page.
One of these bits of info is a user defined message from a textarea, potentially with line breaks. I've encoded the linebreaks as %0A.
I wanted to use $var = $_GET["param"] to retrieve the message and store in a variable, but of course $_GET strips the %0A and replaces with spaces, which is killing the user formatting.
Is there someway I can get this into the variable either with the %0A in tact, or converted to <br>'s.
Thanks for you help.
UPDATE: Here's the code
Example URL:
http://blahblahblah.com/thankyou.php?type=e&gift=1&remail=simon#shokstudio.com&rname=Simon&demail=simon#shokstudio.com&dname=Simon&msg=e.g.%20Dear%20Bob,%20%0A%0AMerry%20Christmas%20and%20a%20Happy%20New%20Year%20to%20you.%20I%20hope%202014%20brings%20you%20much%20joy%20and%20happiness%20to%20you%20and%20your%20loved%20ones.%0A%0ABest%20Wishes,%0A%0ADave%0A%0A%20
PHP processing URL:
<?php
if ( "e" == $_GET["type"]) :
$gift_type = "Ecard";
else :
$gift_type = "PDF";
endif;
$gift_number = $_GET['gift'];
$donor_name = $_POST['dname'];
$donor_email = $_GET['demail'];
$recipient_name = $_POST['rname'];
$recipient_email = $_GET['remail'];
$custom_text = $_POST['msg'];
echo $_POST['msg'];
Use POST instead of GET. This works fine.
form.php
<form method="POST" action="my_form.php">
<input type="text" name="param">
<input type="submit">
</form>
my_form.php
<?php
echo $_POST['param'];
?>
you can either use nl2br or str-replace or preg_replace. But to use POST instead of GET that would be a better and safe solution solution.
Since you are using a web-form with a text-area, i would suggest using the POST method and then using the $_POST (or $_REQUEST) variable instead. All entered data should be in there, with enters and all special characters.

How can I echo an array in an html input text value attribute

I have the following scenario:
I have a PHP array which has tags on it, they can be between 4 and 7.
And I need to put that PHP Array in an input type text separated by commas on the value attribute. Any idea how can I do this?
The input text is a plugin for tags, which is http://timschlechter.github.io/bootstrap-tagsinput/examples/bootstrap3/
Any help would be appreciated!
This is my PHP code:
while($dataTagsToPut = $resultTags->fetch_assoc()){
array_push($stringTags, $dataTagsToPut['SOLUTION_TAGS_NAME']);
}
This is my HTML:
<input type="text" id="solutionTags" value="<?php echo htmlspecialchars(??whatgoeshere); ?>" name="solutionTags">
One simple way:
echo implode(',', array_map('htmlspecialchars', $stringTags));
array_map applies a function to each element and gives you the results from each call. Then you can implode the array to turn it into a string.
Should also work the other way around in this case (implode the array, then call htmlspecialchars on the resulting string), since , isn't special in HTML.
Try this:
<?php echo htmlspecialchars(implode($stringTags)); ?>
Implode the array. implode(',', $stringTags)

PHP: Form variables not registering

I have an HTML form for a limo company's info, kind of like this:
<td>
<input type="text" name="sedan-number-fleet" />
</td>
<td>
<input type="text" name="sedan-year-range" />
</td>
and so on. When I put them into PHP, like so:
$input_sedan_number_fleet = strip_tags($POST['sedan-number-fleet']);
$input_sedan_year_range = strip_tags($POST['sedan-year-range']);
it comes out with no result when I try to echo it. Does it have to do with the strip_tags function? If you know, let me know. That would be great! Thanks!
You don't access form-submitted data through $POST, but $_POST. Change your code to
$input_sedan_number_fleet = strip_tags($_POST['sedan-number-fleet']);
$input_sedan_year_range = strip_tags($_POST['sedan-year-range']);
And also, I hope you do more input validation than just strip_tags.
replace $POST with $_POST. This should do the job. Additionally, use $_GET instead of $GET and so on (read more here)
I was having trouble with this too. Try using " instead of '.
For example:
$input_sedan_number_fleet = strip_tags($_POST["sedan-number-fleet"]);
$input_sedan_year_range = strip_tags($_POST["sedan-year-range"]);
For some reason that worked for me.

$_POST variable name is variable, how to retrieve?

Users of my website can generate a custom form. All the fields are saved in a database with a unique ID. When someone visits the form, the fields 'name' attribute is field*ID*, for example
<p>Your favorite band? <input type="text" name="field28"></p>
<p>Your Favorite color? <input type="text" name="field30"></p>
After submitting the form, I use php to validate the form, but I don't know retrieve the value of $_POST[field28] (or whatever number the field has).
<?
while($field = $query_formfields->fetch(PDO::FETCH_ASSOC))
{
$id = $field[id];
//this doesn't work!!
$user_input = $_POST[field$id];
//validation comes here
}
?>
If anybody can help me out, it's really appreciated!
Add some quotes:
$user_input = $_POST["field$id"];
I'd suggest taking advantage of PHP's array syntax for forms:
<input type="text' name="field[28]" />
You can access this in php with $_GET['field'][28]
$user_input = $_POST['field'.$id];
Remember that you are using a string for the first part of the input name, so try something like: $user_input=$_POST['field'.$id];.
Also, I would suggest calling them into an array to retrieve all data:
<?php
$user_inputs=array();
while($field=$query_formfields->fetch(PDO::FETCH_ASSOC)) {
$id=$field['id'];
$user_inputs[]=$_POST['field'.$id];
}
?>

How do I keep a URL from breaking at the spaces? No method is working

I am having a lot of trouble with this, $report is breaking at the space in the URL, and I Have been trying to fix this problem for a couple of days now, and nothing is working.
<form onsubmit="return validate(this)" method="post" action=
<?
echo "\"reports1.php?report=";
echo rawurlencode($report);
echo "\"";
?>
>
...
if(isset($_GET['report'])){
$report = $_GET['report'];
echo "<script>alert('All reports will be appended to \"".$report."\" until GET and POST data are cleared.')</script>";
}
elseif($country != NULL){
$report = $date." ".$country." ".$topic;
}
elseif($country == NULL){
$report = $date." ".$region." ".$topic;
}
...
Here is an example; the $report is getting $_GET'ted as
"2011-05-08 ",
even though it should be
but it is $_POSTING as "2011-05-08 Brazil Botulism"
"reports1.php?report=2011-05-08 "
urlencode() will work.
Make sure you wrap the address in quotes, and it is on one line:
<form ... action="reports1.php?report=2011-05-08%20Brazil%20Botulism">
Use PHP's trim() function to remove unwanted whitespace in the beginning and the end of a string.
well you dont need to raw it,
echo "hi";
will work fine with ya, in case of ur code above there is only one problem that u use $_POST method for ur form not $_GET
form onsubmit="return validate(this)"
method="post" action=
should be
a very nice and easy debugin code that can help u is retriving all $_GETs and assign them to vars,
with
foreach($_GET as $var=>$val)$$var=$val;
this code wil get all posted gets and assign them to variables with there own name, ($report instead of $_GET['report'];) u can use this function on all $_post and $_get to know where is exactly the problem :)..
hope it helps

Categories