How do I place a unique ID in my PHP confirmation page? - php

I have a PHP script that emails me the results from a form that generates a unique ID number. The PHP script executes a confirmation page. I'm trying to place the unique ID on the confirmation page: quote_confirm.php. I already tried this in the conformation page:
<?php
$prefix = 'LPFQ';
$uniqid = $prefix . uniqid();
$QuoteID = strtoupper($uniqid);
."<tr><td class=\"label\"><strong>Quote ID:</strong></td><td>".$QuoteID."</td></tr>\n"

First off, uniqid() has a prefix parameter, so your line $uniqid = $prefix . uniqid(); should actually be $uniqid = uniqid($prefix);
Also, are you receiving an error? What is the output?

From what I can understand it is something like this:
form -> mail + output page
So there is mail()-like function and after that some output.
If the variable($uniqid) is set you have to make sure it's not overwritten or unset by something else before the actual output. You have to also check the scope of variable.
There is no mistake in the code you posted.
But speaking of style:
I also recommend using using $uniqid = uniqid($prefix) instead of $uniqid = $prefix. uniqid();.
And the following line is strange:
."<tr><td class=\"label\"><strong>Quote ID:</strong></td><td>".$QuoteID."</td></tr>\n"
Write it as "<tr><td class=\"label\"><strong>Quote ID:</strong></td><td>$QuoteID</td></tr>\n" (if you insist on using "") and if the function is echo (example: echo $something not $content .= $something) use , instead of . .
Full example:
echo 'Constant text without variables', $variable, "String with a $variable and a newline \n";

Related

What is wrong with my 'Save File' php code?

The question is simple but i will give some background information to hopefully make answering it easier.
So, I am working with the ELGG framework and taking information from a form and the text boxes in it in hopes to print that data to a simple text file.
Unfortunately, for some reason this has been giving me lots of trouble and I cannot seem to figure out why no matter where I look.
Code is as followed
<?php
error_reporting(E_ALL);
//Get the page
$title = get_input('title');
$body = get_input('body');
$date = date("l jS F Y h:i A");
//remove any possible bad text characters
$FileName = $title . ' ' . $date;
//print to file
//get filename and location to save
$folderName = '/Reports/' . $FileName . '.txt';
$ReportContent = "| " . $body . " |";
//write to the file
file_put_contents($folderName, $ReportContent, 0);
//error check to see if the file now exists
if (file_put_contents($folderName, $ReportContent)) {
system_message("Report Created (" . basename($folderName) . ")");
forward(REFERER);
} else {
register_error("Report (" . basename($folderName) . ") was not saved!");
forward(REFERER);
}
So what above SHOULD do is grab the text from the title and body box (which i can confirm it does from the title at least) and then save it under the /reports/ folder (full path for the plugin is Automated_script_view/reports/ if needed). Yet I will always get the register error, and I cannot seem to find why.
I believe it has to do with the declaration of the folder (/reports/), as if I take that part away, it passes and submits, although it doesn't seem to actually save anywhere.
Any and all advice would be very much appreciated!
The Function file_put_contents(file,data,mode,context) returns the number of character written into the file on success, or FALSE on failure. Note the following corrections and your script will work just fine:
1 Your File name has an 'illegal' character ":" coming from the $date part of the string you concatenate to form the filename.
2 Remove file_put_contents($folderName, $ReportContent, 0); Since the function returns an integer, simple use:
if( file_put_contents($folderName, $ReportContent) > 0 ){
//true
}else{
//false
}

Make a $_GET into variable or place into URL

How do you place a $_GET['****']; into a string or make it into a variable.
For Example i have this url:
http://localhost/PhpProject2/product_page.php?rest_id=3/area=Enfield.
I want to get the area and rest_id from the url. in order to redirect another page to this exact page.
echo"<script>window.open('product_page.php?rest_id= 'put get here'/area='put get here'','_self')</script>";
I have so far done this:
if(isset($_GET['rest_id'])){
if(isset($_GET['rest_city'])){
$_GET['rest_id'] = $rest_id;
}
}
This obviously does not work, so my question is how do i make the 2 $_GET into a variable or call the $_GET into the re-direct string.
What i have tired so far
echo"<script>window.open('product_page.php?rest_id=' . $GET['rest_id'] . '/area='put get here'','_self')</script>";
How or what is the best practice?
ok, first things first. in your URL you have to separate the parameters using an ampersand "&", like this
http://localhost/PhpProject2/product_page.php?rest_id=3&area=Enfield
Also, you have to assign the $_GET value to a variable, not the other way around, like this
$rest_id = $_GET['rest_id'];
so if you create a PHP file named product_page.php and use the url i gave you, and your PHP code looks like this, it should work..
<?php
if (isset($_GET['rest_id'])){
$rest_id = $_GET['rest_id'];
}
if (isset($_GET['rest_id'])){
$area = $_GET['area'];
}
$url = 'other_page.php?rest_id=' . $rest_id . '&area=' . $area;
header("Location: $url");
?>
The question here is why do you want to redirect from this page to the other, and not send the parameters directly to the "other_page.php"????

PHP: Include contents of tile - preserve $_POST variables

I've got a script that sends out emails.
It goes through a for loop, where it finds all subscribers, gets their name and sets a POST var with their name in it.
Next I've got a separate PHP file which contains the markup of the email and content.
It has a $_POST['fname'] var which is the users name and needs to be updated dynamically.
The problem is I'm unsure how to grab the contents of my email file (template.php) and add it into the email processing file (bulk_send.php) so that the $_POST['fname'] var can be dynamically updated. Here's the code.
foreach ($emailList as &$value) {
$getSubscriber = mysql_query("SELECT * FROM subscribers WHERE email = '$value' ");
while($row = mysql_fetch_assoc($getSubscriber)){
$_POST['fname'] = $row['fname'];
$_POST['lname'] = $row['lname'];
}
//WHERE IM STUCK
$bodyText = ***INCLUDE template.php .....***
//Code that will send out the email with $bodyText as the body of the email
}
The contents of our template.php is something simple - lets just say something like:
<div><?php echo $_POST['fname')." ".$_POST['lname]; ?></div>
How could I include template.php file appropriately in the Foreach loop above so that the POST vars are updated with each iteration?
You can't quite directly set a variable equal to the contents of another file using include as suggested by Mr.coder in a comment.
You can think of include as copying and pasting the contents of the included file. Based on your template.php, it would be as if your code were:
while($row = mysql_fetch_assoc($getSubscriber)){
$_POST['fname'] = $row['fname'];
$_POST['lname'] = $row['lname'];
}
?><div><?php echo $_POST['fname']." ".$_POST['lname']; ?></div><?php
That would simply dump the div contents out to the browser, which is probably not what you want. Instead, I'd suggest building into your template a function which would create the message text you desire based on parameters fed to the function. Then you would include your template at the top of your page and call the function when needed. It would look more like this:
include('template.php');
foreach ($emailList as &$value) {
$getSubscriber = mysql_query("SELECT * FROM subscribers WHERE email = '$value' ");
while($row = mysql_fetch_assoc($getSubscriber)){
$_POST['fname'] = $row['fname'];
$_POST['lname'] = $row['lname'];
}
//WHERE IM STUCK
$bodyText = makeBodyText($_POST['fname'], $_POST['lname']);
//Code that will send out the email with $bodyText as the body of the email
}
Your template.php would then look something like this:
function makeBodyText($fname, $lname)
{
return '<div>' . $fname .' '. $lname . '</div>';
}
Alternatively, because the included file does operate in the same scope as where it where was called, and because it does support the concept of a return value (thanks to user #kokx for that insight), you could make your template.php like this:
return '<div>' . $_POST['fname'] .' '. $_POST['lname] . '</div>';
And then you could use it as follows:
$bodyText = include 'template.php';
However, that significantly limits the flexibility of template.php. Also, it would potentially output bad information if called directly (from a browser, rather than as an include). So I would not recommend this method.
Now all that aside, it seems odd to me that you are modifying the contents of $_POST. That strikes me (and others) as bad practice.

HREF to call a PHP function and pass a variable?

Is it possible to create an HREF link that calls a PHP function and passes a variable along with it?
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='search($id)' >$name</a>";
}
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.
So for example, www.google.com/search.html?term=blah
Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".
Multiple variables can be separated with a &
So for example, www.google.com/search.html?term=blah&term2=cool
The GET method is independent of PHP, and is part of the HTTP specification.
PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.
Here is some demo code to show how this works:
<?php
//check if the get variable exists
if (isset($_GET['search']))
{
search($_GET['search']);
}
function Search($res)
{
//real search code goes here
echo $res;
}
?>
Search
which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets
The HTML output needs to look like
anchor text
Your function will need to output this information within that format.
No, you cannot do it directly. You can only link to a URL.
In this case, you can pass the function name and parameter in the query string and then handle it in PHP as shown below:
print "<a href='yourphpscript.php?fn=search&id=$id' >$name</a>";
And, in the PHP code :
if ($_GET['fn'] == "search")
if (!empty($_GET['id']))
search($id);
Make sure that you sanitize the GET parameters.
No, at least not directly.
You can link to a URL
You can include data in the query string of that URL (<a href="myProgram.php?foo=bar">)
That URL can be handled by a PHP program
That PHP program can call a function as the only thing it does
You can pass data from $_GET['foo'] to that function
Yes, you can do it. Example:
From your view:
<p>Edit
Where 1 is a parameter you want to send. It can be a data taken from an object too.
From your controller:
function test($id){
#code...
}
Simply do this
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='?search=" . $id . "' > " . $name . "</a>";
}
}
if (isset($_REQUEST['search'])) {
search($_REQUEST['search']);
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
Also make sure that your $json_output is accessible with is the sample() function. You can do it either way
<?php
function sample(){
global $json_output;
// rest of the code
}
?>
or
<?php
function sample($json_output){
// rest of the code
}
?>
Set query string in your link's href with the value and access it with $_GET or $_REQUEST
<?php
if ( isset($_REQUEST['search']) ) {
search( $_REQUEST['search'] );
}
function Search($res) {
// search here
}
echo "<a href='?search='" . $id . "'>" . $name . "</a>";
?>
Yes, this is possible, but you need an MVC type structure, and .htaccess URL rewriting turned on as well.
Here's some reading material to get you started in understanding what MVC is all about.
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
And if you want to choose a sweet framework, instead of reinventing the MVC wheel, I highly suggest, LARAVEL 4

Get user input from form, write to text file using php

As part of a subscriber acquisition I am looking to grab user entered data from a html form and write it to a tab delimited text file using php.The data written needs to be separated by tabs and appended below other data.
After clicking subscribe on the form I would like it to remove the form and display a small message like "thanks for subscribing" in the div.
This will be on a wordpress blog and contained within a popup.
Below are the specific details. Any help is much appreciated.
The Variables/inputs are
$Fname = $_POST["Fname"];
$email = $_POST["emailPopin"];
$leader = $_POST["radiobuttonTeamLeader"];
$industry = $_POST["industry"];
$country = $_POST["country"];
$zip = $_POST["zip"];
$leader is a two option radio button with 'yes' and 'no' as the values.
$country is a drop down with 40 or so countries.
All other values are text inputs.
I have all the basic form code done and ready except action, all I really need to know how to do is:
How to write to a tab delimited text file using php and swap out the form after submitting with a thank you message?
Thanks again for all the help.
// the name of the file you're writing to
$myFile = "data.txt";
// opens the file for appending (file must already exist)
$fh = fopen($myFile, 'a');
// Makes a CSV list of your post data
$comma_delmited_list = implode(",", $_POST) . "\n";
// Write to the file
fwrite($fh, $comma_delmited_list);
// You're done
fclose($fh);
replace the , in the impode with \t for tabs
Open file in append mode
$fp = fopen('./myfile.dat', "a+");
And put all your data there, tab separated. Use new line at the end.
fwrite($fp, $variable1."\t".$variable2."\t".$variable3."\r\n");
Close your file
fclose($fp);
// format the data
$data = $Fname . "\t" . $email . "\t" . $leader ."\t" . $industry . "\t" . $country . "\t" . $zip;
// write the data to the file
file_put_contents('/path/to/your/file.txt', $data, FILE_APPEND);
// send the user to the new page
header("Location: http://path/to/your/thankyou/page.html");
exit();
By using the header() function to redirect the browser you avoid problems with the user reloading the page and resubmitting their data.
To swap out the form is relatively easy. Make sure you set the action of the form to the same page. Just wrap the form inside a "if (!isset($_POST['Fname']))" condition. Put whatever content you want to show after the form has been posted inside the "else{}" part. So, if the form is posted, the content in the "else" clause will be shown; if the form isn't posted, the content of the "if (!isset($_POST['Fname']))", which is the form itself will be shown. You don't need another file to make it work.
To write the POSTed values in a text file, just follow any of the methods other people have mentioned above.
This is the best example with fwrite() you can use only 3 parameters at most but by appending a "." you can use as much variables as you want.
if isset($_POST['submit']){
$Fname = $_POST["Fname"];
$email = $_POST["emailPopin"];
$leader = $_POST["radiobuttonTeamLeader"];
$industry = $_POST["industry"];
$country = $_POST["country"];
$zip = $_POST["zip"];
$openFile = fopen("myfile.ext",'a');
$data = "\t"."{$Fname}";
$data .= "\t"."{$email}";
$data .= "\t"."{$leader}";
$data .= "\t"."{$industry}";
$data .= "\t"."{$country}";
$data .= "\t"."{$zip}";
fwrite($openFile,$data);
fclose($openFile);
}
Very very simple:
Form data will be collected and stored in $var
data in $var will be written to filename.txt
\n will add a new line.
File Append Disallows to overwrite the file
<?php
$var = $_POST['fieldname'];
file_put_contents("filename.txt", $var . "\n", FILE_APPEND);
exit();
?>

Categories