Show in the email from which page the form was submitted - php

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.

Related

Place a default email address in when a custom form field is left blank

I want to place a default email address in when a custom form field is left blank. I can't get the code right. I use email address in place of the real email address.
<?php if(get_field('cemail')) { ?>
<?php
$email = (get_field('cemail'));
if($email!=""){
echo 'email address' ;
}
?>
You can do this like;
<?php
$email = $_GET["email"];
if($email=="" || !filter_var($email, FILTER_VALIDATE_EMAIL) === false)
$email = "default_email#email.com";
echo $email;
?>
If you just want to substitute a default value when the email field is blank. You can do this:
$email = (isset($_GET['cemail'])) ? $_GET['cemail'] : "defaultemail#email.com";
Don't forget to make sure submitted values are valid email addresses and always clean your inputs if values will go into a database!
How can I display a default value if the field was empty?
I assume that your procedural method get_field() simply just returns the value of a global variable GET or POST parameter so...
This can be achieved in 1 line with something called a ternary expression, an example is displayed below.
<form action="/member.php" method="post">
<input name='cemail' value="<?php echo (!empty(get_field('cemail')) ? get_field('cemail') : 'default#me.com'; ?>" id="cemail">
</form>
We're firstly checking if the field is not empty (!) and then using that result inside the ternary expression to dictate what to do.
Here are some sources to help you understand further what's happening above:
Ternary Expressions
PHP Empty Syntax
Try this:
<?php if(get_field('cemail'))
{
$email = (get_field('cemail'));
if(trim($email) == "")
$email = 'default#email.com';
}
echo $email;
}
?>

capture information from html page and popup window

i have an html page with text filed , and an action button to open a popup like this example
http://www.andwecode.com/playground-demo/pop-up-login-signup-box-jquery/#modal
(hit login to see the popup) who had also a two text fields , how i can collect the text entered in the 3 boxes (the one in the page and the two in the popup window) simultaneously with a php file ? cause i need to send them all to my email
an exemple of my php file :
<?php
$txt1 = "textfield 1 : ".$_POST['textfield1'];
$txt2= "textfield 2 : ".$_POST['textfield2'];
$tx3= "textfield 3 : ".$_POST['textfield3'];
$message = "
$txt1
$txt2
$txt3
";
$to = "myemail#example.com";
$subject = "data :".$txt1;
$headers = "From: <myemail#example.com>";
$headers = "MIME-Version: 1.0\n";
$from = "example";
mail($to,$subject,$message,$headers,$from);
}
?>
any idea how to collect all the text from the
For PHP to receive the values of three different inputs in a single POST, all three inputs need to be contained within the same HTML <form>. Try moving HTML around so that all inputs are contained in a single form, then they should all be accessible to PHP in the $_POST array.
It seems to me you don't have 'name' attributes on the input tags, try something like this,
<!--ALL THE HTML-->
Email: <input type="text" name="email"></input>
Password: <input type="pass" name="pass"></input>
<!--MORE HTML-->
SEPARATE PHP FILE!!
<?php
function getdata(){
$email= $_GET['email']; //Email
$pass= $_GET['pass']; //Password
$name = $_GET['name']; //Full name (Only will apply if registering)
};//This gets the data and gives it a variable
//Then more php like you had already to email to yourself
?>
In simple terms, add the 'name' attribute to the inputs (name="whatevername") and then use the $_GET in php to get the data ($varname = $_GET['nameattributehere'];).
Just remember!
To put the PHP code in a separate file with the .PHP extension on it (.HTML doesn't work!)
Add the 'name' attributes to the inputs
And on the <form> tag you MUST ADD THIS! method="get" action="srcforthephpfile.php"
Good Luck

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.

Assigning a variable a value from an recordset

I apologize but I'm very new to PHP and I am trying to create a very simple form that sends an email back to a user when they enter in their email address. I want the message to include some data from our database. I have been able to create a form that works perfectly as long as I enter in the message manually (like $message = "Hi. How you doing?") but I can't seem to figure out how to incorporate the recordset data. What I was hoping was to use something like...
<?php
$to = $_REQUEST['Email'] ;
$message = '<?php echo $row_rsPersonUser['bio']; ?>'; <<<<<<<<Line 63
$fields = array();
$fields{"Email"} = "Email";
$headers = "From: noreply#domain.ca";
$subject = "Thank you";
mail($to, $subject, $message, $headers);
?>
What I get from this is "Parse error: syntax error, unexpected T_STRING in.... on line 63". I know it's formatted wrong but I don't have a clue why. When I drop the into the body, the info I want does display on the webpage so I know that part is working. Any help would be welcomed.
Thanks
You don't have to use PHP start and end tags inside PHP code itself
$message = '<?php echo $row_rsPersonUser['bio']; ?>'; // this is wrong
^^^^^ ^^
Should be
$message = $row_rsPersonUser['bio'];
Just change the 63rd line like below..
you can't start one <?php block in another <?php block
$message = $row_rsPersonUser['bio'];
If you were to do that it would just print <?php echo… as literals since you can't send php code as a email only html/plan text
$message = '<?php echo $row_rsPersonUser['bio']; ?>';
should be:
$message = $row_rsPersonUser['bio'];
and (I tested the following and it appears the {}'s work but you might want to switch to only []'s for standardization and not sure if you might get in trouble later on?)
FROM: http://us1.php.net/manual/en/language.types.array.php
Note:
Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).
$fields{"Email"} = "Email";
should be:
$fields["Email"] = "Email";
You are already inside the php code, no need to add extra php start and end tags inside the variable name. Similar to how you have used the $to variable, you can use the $message variable.
So use
$message = $row_rsPersonUser['bio'];
and it would work fine.

Cant get field values with space from mysql using php

I am trying to pass variable that containing the values with space through href but I fail to get the expected output with space.
The code I used is:
print " <a href=update.php?id='$id'&name=$name&dob='$dob'&email='$email'>Update Details</a> <br>
Student ID: $id<br> Student Name: $name<br> Date Of Birth: $dob<br> Email ID: $email<br>";
In update.php I could see the link as
localhost/student_portal/update.php?id='abc'&name=Giridharan
and I didn't get the full name and dob and email
My variables with values are as follows:
$id=abc
$name=Giridharan Rengarajan
$dob=1993-07-22
$email=rgiridharan.93#gmail.com
What should I do to get all the four values in update.php?
Since spaces are not legal parts of the query string you have to encode them.
Eg:
Use rawurlencode / rawurldecode
<a href=update.php?id='$id'&name=rawurlencode($name)&dob='$dob'&email='rawurlencode($email)'>Update Details</a>
You can get the variables in update.php by:
<?php
$id = $_GET['id'];
$name = $_GET['name'];
$dob = $_GET['dob'];
$email = $_GET['email'];
For proper creation of url you can use http_build_query.
See examples here http://www.php.net/manual/en/function.http-build-query.php
Create array of your params, put it into this function and to your update script like a string.

Categories