Note that there are some explanatory texts on larger screens.

plurals
  1. POHide web page if no variable present in the url
    text
    copied!<p>I have created a form that pulls a variable from a url using the following code:</p> <p><code>&lt;?php $id = $_GET['id']; echo $id; ?&gt;</code></p> <p>Using this is link as an example (http://www.mydomain.co.uk/customerform.php?id=12) it would display 12 on the webpage.</p> <p>What I would like to know, is it possible to stop the web page from loading if there is no varible in the URL e.g ?id=12 ?</p> <p>Either in php, javascript?</p> <p>So If you visited the following link, the web page would display:</p> <p><a href="http://www.mydomain.co.uk/customerform.php?id=12" rel="nofollow">http://www.mydomain.co.uk/customerform.php?id=12</a></p> <p>and if you visited this link (without the variable), the page would not display:</p> <p><a href="http://www.mydomain.co.uk/customerform.php" rel="nofollow">http://www.mydomain.co.uk/customerform.php</a></p> <p>Thank you for any help.</p> <p><strong>Update below...</strong></p> <p>I have added Krishnanunni's code and it successfully prevents the web page from displaying if the is no vartiable in the url. But adding this code prevents the form submitting...</p> <p>Is there a way of keeping the url variable when the form is submitted?</p> <p>Can anyone help? Thanks</p> <pre><code>&lt;body id="main_body" &gt; &lt;div id="form_container"&gt; &lt;? /* only load page is variable is presnt in the URL */ if (!isset($_GET['id']) || empty($_GET['id'])) { die("Bad Request"); } ?&gt; &lt;? /* Mailer with Attachments */ $action = $_REQUEST['action']; global $action; function showForm() { ?&gt; &lt;form id="contact" class="appnitro" name="idform" enctype="multipart/form-data" method="post" action="&lt;?=$_SERVER['PHP_SELF']?&gt;"&gt; &lt;input type="hidden" name="action" value="send" /&gt; &lt;input type="hidden" name="MAX_FILE_SIZE" value="10000000"/&gt; &lt;input name="to_email" type="hidden" id="to_email" value="myemail@domain.co.uk"/&gt; &lt;div&gt; &lt;input id="invoice" name="invoice" class="element text medium" type="hidden" maxlength="255" value="&lt;?php $id = $_GET['id']; echo $id; ?&gt;"/&gt; &lt;/div&gt; &lt;div&gt; &lt;input id="subject" name="subject" class="element text medium" type="hidden" maxlength="255" value="Enquiry Form - (Invoice: &lt;?php $id = $_GET['id']; echo $id; ?&gt;)"/&gt; &lt;/div&gt; &lt;label class="description" for="from_name"&gt;Name &lt;/label&gt; &lt;div&gt; &lt;input id="from_name" name="from_name" class="element text medium" type="text" maxlength="255" value=""/&gt; &lt;/div&gt; &lt;label class="description" for="position"&gt;Position &lt;/label&gt; &lt;div&gt; &lt;input id="position" name="position" class="element text medium" type="text" maxlength="255" value=""/&gt; &lt;/div&gt; &lt;label class="description" for="uemail"&gt; Email &lt;/label&gt; &lt;div&gt; &lt;input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/&gt; &lt;/div&gt; &lt;label class="description" for="phone"&gt; Phone &lt;/label&gt; &lt;div&gt; &lt;input id="phone" name="phone" class="element text medium" type="text" maxlength="255" value=""/&gt; &lt;/div&gt; &lt;input name="Reset" type="reset" class="contactform_small" value="Reset" /&gt; &lt;input name="Submit2" type="submit" class="contactform_small" id="Submit" value="Submit" /&gt; &lt;/form&gt; &lt;? } function sendMail() { if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy! die ("&lt;p&gt;Oops! You forgot to fill out the email address! Click on the back arrow to go back&lt;/p&gt;"); } else { $to_name = "MY BUSINESS"; $from_name = stripslashes($_POST['from_name']); $subject = Trim(stripslashes($_POST['subject'])); $to_email = $_POST['to_email']; $EmailFrom = Trim(stripslashes($_POST['email'])); // get posted data into local variables $position = Trim(stripslashes($_POST['position'])); $position2 = Trim(stripslashes($_POST['position2'])); $phone = Trim(stripslashes($_POST['phone'])); $invoice = Trim(stripslashes($_POST['invoice'])); //Let's start our headers $headers = "From: $EmailFrom&lt;" . $_POST['email'] . "&gt;\n"; $headers .= "Reply-To: &lt;" . $_POST['email'] . "&gt;\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; $headers .= "X-Sender: $from_name&lt;" . $_POST['email'] . "&gt;\n"; $headers .= "X-Mailer: PHP4\n"; $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal $headers .= "Return-Path: &lt;" . $_POST['email'] . "&gt;\n"; $headers .= "This is a multi-part message in MIME format.\n"; $headers .= "------=MIME_BOUNDRY_main_message \n"; $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; $message = "------=MIME_BOUNDRY_message_parts\n"; $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; $message .= "Content-Transfer-Encoding: quoted-printable\n"; $message .= "\n"; /* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */ $message .= "Invoice: "; $message .= $invoice; $message .= "\n"; $message .= "\n"; $message .= "My Business Enquiry Form"; $message .= "\n"; $message .= "\n"; $message .= "Contact Details: "; $message .= "\n"; $message .= "\n"; $message .= "Name: "; $message .= $from_name; $message .= "\n"; $message .= "Position: "; $message .= $position; $message .= "\n"; $message .= "Email: "; $message .= $EmailFrom; $message .= "\n"; $message .= "Phone: "; $message .= $phone; $message .= "\n"; $message .= "\n"; // send the message mail("$to_name&lt;$to_email&gt;", $subject, $message, $headers); print "Form sent successfully"; } } ?&gt; &lt;? switch ($action) { case "send": sendMail(); showForm(); break; default: showForm(); } ?&gt; &lt;div id="footer"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload