Download article to visitors computer

Hi,

This is probably very simple. I’m not sure whether I actually need php but this is what i want to do.

I have a website at web-writer-articles.co.uk

I want the visit to to be able to download a free 10 page article to their computer

The process would be they click on the link and they are taken to a page that asks them for their name and email. Once they click submit the article is downloaded to their computer.

I know how to insert the name and email into the database etc its more how the next stage works.

I suppose another way would be to send download info through email?

thanks for your thoughts,

regards,

Nick

if you want to handle the form submission and display of a download link without a page refresh then you can do it with AJAX

but if you want to make sure it will work in javascript disabled browsers then you could

  1. when the submit button is clicked, send name and email address to a form processing script (formProcessor.php)

  2. in formProcessor.php after you have validated, sanitised and inserted the name and email into the database, use php to change the display style of a normally hidden download link to block to make it visible.

  3. set the href of the download link to the file you want to download.

Hi Kalon

Thanks for getting back to me. I don’t know AJax and I guess I am a little wary about javascript the way you suggested using aphp form script looks the way to go.

but my knoledge around constructing php forms kind of begins and ends at validation, sending info to database, putting up thank you page, sending email to client to confirm and sending an email to me to say someone has but something in the database.

could you explain a litle further how I would change display visisbiliuty and how it all actually ads on to the script. Or if you could point me in the direction of a good tutorial that would be great.

thanks:)

ok, one way you could it is

  1. in formProcessor.php (from previous post) set a flag saying whether it’s ok to display the download link to false.
 
$isOkToDownload = false;

  1. after the name and email address has been added to the database set
 
$isOkToDownload = true;

  1. then in the <head> of formProcessor.php set the style of the container of the download link to block.

example:

let’s say you have a div with an id = “dwnLoadLinkCont” in the <body> somewhere.

Then

 
<?php
echo '<style type="text/css">';
echo '#dwnLoadLinkCont {display:'.($isOkToDownload)? 'block' : 'none'.'}';
echo '</style>';
}
 
?>

sorry , not sure what you mean by a flag. do i need a function here?

How would it fit into a script like this:


<?php require_once("connect/connection.php");
require_once("includes/functions.php");
if(isset($_POST['submit'])){
$errors = array();
$requiredfields = array('first_name'=> 'Please enter your FIRST NAME','surname'=> 'Please enter your SURNAME','email'=> 'Please enter your E Mail ADDRESS','text'=> 'Please enter your current needs');
foreach($requiredfields as $fieldname => $requiredfieldsmessage){
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) || strlen(trim($_POST[$fieldname])) == 0) {
$errors[] = $requiredfieldsmessage;
}
}
 if(count($errors) == 0){
 
 $_POST = array_map('mysqli_prep', $_POST);
 
$first_name =  mysqli_prep($_POST['first_name']);
$surname =  mysqli_prep($_POST['surname']);
$email =   mysqli_prep($_POST['email']);
$text =  mysqli_prep($_POST['text']);
$query = "INSERT INTO form (first_name,surname,email,text)Values('{$first_name}','{$surname}','{$email}','{$text}')";
$result = mysqli_query($connection, $query);
$message="A contact form has been completed on web-writer-articles.co.uk";
$subject="Ghostwriter contact form";
$subject2="Nick Cassells: Ghostwriter and copywriter";
$message2="Thank you for contacting me concerning your copywriting or ghostwriting needs. I will get back to you at this email address as soon as possible.<br> With best regards,<br> Nick Cassells";
$email2=$email;
mail('njc27@sky.com',$subject,$message);
mail($email2, $subject2, $message2);
header('Location: brief_return.php');
exit;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
<div id="content">
<div id="mainContent">
<div id="contactform"><?php
if(count($errors) != 0){ 
echo '<h3>Sorry, please address the following errors:-</h3>';
echo '<ul>'; 
foreach ($errors as $error) { 
echo '<li>' . $error . '</li>';}echo '</ul>';
}
?><h3 class="contacttext">For a <span style="color:red">free</span> pdf guide about your manuscript or a <span style="color:red">free </span>web site review for your web content needs please fill in the contact form below. h3>
<p class="contacttext">Telephone: 01795 843184
<a href="mailto:njc27@sky.com">Email</a>
or fill in the form below and I will get back to you as soon as possible:</p>
<form style="padding:15px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >

<p>First Name:<br/> <input type="text" size="30" name="first_name" value="<?php echo htmlspecialchars($_POST['first_name'],ENT_QUOTES); ?>" id="fname" /></p>
<p> Surname:<br/><input type="text" size="30" name="surname" value="<?php echo htmlspecialchars($_POST['surname'],ENT_QUOTES); ?>" id="sname" /></p>
<p>E mail:<br/><input type="text" size="40" name="email" value="<?php echo htmlspecialchars($_POST['email'],ENT_QUOTES); ?>" id="email" /></p>
  <input type="submit" name="submit" value="Submit " />
</form>
</div>


thanks for your time,

regards

Nick

by a “flag” I mean just a boolean variable whose value is true or false

 
$isOkToDownload = false;  //initialise flag
 
/*
your code to validate, sanitise and insert the form data into your database.
 
then when the data has been inserted to the database.
*/
 
$isOkToDownload = true;
 
//you might have some more code here to do stuff
 
//then display the download link
 
if($isOkToDownload) {
 
      //code to display the link
 
}
 

thanks so much this is making sense now. Just one thing
When you put

if($isoktodownlod){

Shouldn’t this be

if{$isoktodownload=true)

thanks

nick

The test condition in an IF statement must evaluate to true or false and so in

 
if($isoktodownlod){
 

the test condition, in this case just simply $isoktodownlod, will evaluate to true or false since the variable is a boolean.

the above notation is just a short cut of what is probably the better way of doing it

 
if($isoktodownlod [===](http://php.net/manual/en/language.operators.comparison.php) true){
 

thanks for sticking with this kalon

You’ve helped me move foreward

have a feally good weekend

nick

you’re welcome :slight_smile: and you too.