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

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.

Related

PHP (json_encode, implode) store data in a txt file

I'm trying to store data in a .txt file..
The data is already appear on my HTML page but I couldn't know how to post them in a txt file or store them in a session.
In main page:
<?php
echo implode('<br/>', $res->email);
echo json_encode($res->password);
?>'
I want to do something like below:
<?php
$login = "
EMAIL : $_POST['$res->email'];
PASSWORD: $_POST['$res->password']; ";
$path = "login.txt";
$fp = fopen($path, "a");
fwrite($fp,$login);
fclose($fp);
?>
So this $_POST['$res->email']; doesn't work with me I get in the login.txt:
EMAIL : json_encode(Array)
PASSWORD: implode('<br/>', Array)
Neither function calls nor $_POST['$res->email'] would work in string/interpolation context.
While unversed, you should assemble your text data line by line:
$login = " EMAIL : "; # string literal
$login .= implode('<br/>', $res->email); # append function/expression result
$login .= CRLF; # linebreak
$login .= " PASSWORD: "; # string literal
$login .= json_encode($res->password); # append function/expression result
$login .= CRLF; # linebreak
And instead of the oldschool fopen/fwrite, just use file_put_contents with FILE_APPEND flag.
When you use post data you recieve it in your php file. You dont send post data from a php file. With that in mind you manipulate this data with php in the following way:
If is data you recieved from post:
echo $_POST['field'];
This will show the message stored on the field variable among the posted data. But check that the field will be always a string (even though the contents may not be so)
If you want to acces dynamically a field just have in mind that it should be a string for example:
$email = "example#gmail.com";
echo $_POST[$email]
This will NOT return the posted email, but will return the contents from a variable inside Post called "example#gmail.com". Which is the same as :
echo $_POST["example#gmail.com"];
But making now a correct example. if you have this html in your webpage
<form action="/yourphp.php" method="post" target="_blank">
<input type="text" name="email">
<input type="submit" value="Submit">
</form>
you will be able to recover the data from the input field named "email"
echo $_POST['email'];
and this will return the email inside the input.
After you have this clear, you can manipulate the data in different ways to send them to a file, but usually you will have to instantiate a handler, open a file, write content, save and close the file, all depending on your handler.

Show in the email from which page the form was submitted

I have a html form on every page and I need to be able to show in the receiver email from which page the visitor submitted the form. How can I achieve this in PHP? I have tried using $_SERVER['REQUEST_URI'] whatsoever, but it just simply doesn't output anything. I'm using Wordpress.
<?php
global $post;
$post_slug=$post->post_name;
$name = $_POST['firstname'];
$email = $_POST['email'];
$message="$name.$email";
mail('example#gmail.com', "Hello", "$name \n $email \n $_SERVER['REQUEST_URI']");
echo "works";
?>
Your code is fine except, you should enclose array variables inside strings with curled braces {}:
mail('example#gmail.com', "Hello", "$name \n $email \n {$_SERVER['REQUEST_URI']}");
If you check the official php documentation on: http://php.net/manual/en/language.types.string.php#language.types.string.parsing you can see in section "Complex (curly) syntax":
// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";
you can get the actual url with :
$actual_link = "http(s)://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
You can try something this . Pass the title of your page ( or the permalink ) to a hidden html input and take the values as :
$titlePage = get_the_title($post->ID);
<input type="hidden" value="<?php echo $titlePage; ?>" name="pagetitle">
Then in your email code :
$pagetitle = $_POST['pagetitle'];
Now you have your parameter, use it in your email like you want.

Passing a Value from PHP to Javascript Variable

I've seen numerous posts on how to do this either saying to use rawurlencode in the php and decodeURIComponent in javascript or just use json_encode. Neither work for me. Hoping someone can see what I'm doing wrong:
I have an html button like this:
<button id="editbutton" onClick='edit(this, "<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>", "<?php echo $result_cameras[$i]["camera_type"]; ?>", "<?php echo rawurlencode($result_cameras[$i]["camera_name"]); ?>")'>Edit</button>
I pass that into the javascript edit button:
var edit = function(t, to, cameratype, cameraname, cameraquality, camerahash, camerastatus, emailnotice, camerasensitivity, axisuser, axispass, axisip, axisport)
{
if (mydiv.find("form").length) {
mydiv.show();
} else {
// fields
var $myform = $("<form id='EditCameraForm' name='' method='post' action='" + to + "'></form>");
var $myfieldset = $("<fieldset><legend>Edit camera settings</legend></fieldset>");
var $myinput = $('<input/>').attr('type','hidden').attr('name','camera_type').val(cameratype);
var $mylabel = $("<label for='CameraName'>Camera name: </label>");
var $myinput2 = js('<input/>').attr('size','25').attr('name','camera_name').attr('id','CameraName').val(decodeURIComponent(cameraname));
$myform.append($myinput, $mylabel, $myinput2);
...
}
...
}
I've tried using rawurlencode/decodeURIComponent as above and when I hit the edit button if the camera name is called: a"a (just testing the quotes) I get a"a. Backslashes such as a\b just returns some weird characters back.
If I try:
<?php echo json_encode($result_cameras[$i]["camera_name"]); ?>
and don't put anything in the javascript code I get this error:
missing ) after argument list
edit(this, "/dashboard", "WEBCAM", ""a"a"", "0", "3dd10c49784e2207de1e1932958bfb...
Where it is pointing to the ""a"a"".
Any suggestions?
You are in effect outputting a javascript string literal, so using htmlentities is not the correct thing to do. What you need instead is addslashes:
<?php echo addslashes($_SERVER['REQUEST_URI']); ?>
Edit: It goes without saying that you also need to do this for the other two strings you are echoing.
The reason this is correct is that the escape sequences for Javascript string literals are compatible with what addslashes does, if you ignore the fact that addslashes also escapes the "null" character. However, there's no way that character will be part of your URL so there is a perfect match between what addslashes does and what Javascript expects from its string literals.
For completeness I should mention that an appropriate usage of htmlentities is to process text that is being sent as part of HTML content; even then, htmlspecialchars (which performs a small subset of the work of htmlentities) is almost always the better fit.
rawurlencode() is for converting a string to an URL argument
htmlentities() is for converting a string to an HTML content
If you'd like to convert a string to a Javascript string it should be :
function f_str_2js($x) {
return str_replace( array("\n","\r","\t","'",'"') , array('\n','\r','\t',"\\'",'\"') , $x)
}
the above answers are correct, but you should consider to add your data to a php array or object, than jsonencode and add it to a date-attribute date-camera='{... than do eventbinding by using jQuery .on(.
This way you can access the data via $(this).data('camera'). It will be easy to extend you app without adding more and more parameters to the onclick-function.
for more information see:
http://api.jquery.com/jQuery.data/
http://api.jquery.com/on/

Using $_GET funcion instead of using plain text

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

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