Results 1 to 3 of 3

Thread: Php select by list item

  1. #1
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Php select by list item

    Hi all,

    I have a problem I have been trying to work out, wonder if anyone could shed some light?

    I have an include file with an unordered list:

    Code:
    <ul>
     
    <li><a href="quotation_request.php">Turf supplied to order</a></li>
    <li><a href="quotation_request.php">Lawn preparation and turf laying</a></li>
    <li><a href="quotation_request.php">Stonewalling</a></li>
    <li><a href="quotation_request.php">Brick-paved driveways</a></li>
    <li><a href="quotation_request.php">Hedge laying</a></li>
    <li><a href="quotation_request.php">Garden planning</a></li>
    <li><a href="quotation_request.php">Garden sheds and benches</a></li>
     
    </ul>
     
    </div>
     
    <div class="btmritnav">
     
    <ul>
     
    <li><a href="quotation_request.php">Timber fences</a></li>
    <li><a href="quotation_request.php">Rockery creation</a></li>
    <li><a href="quotation_request.php">Water features and ponds</a></li>
    <li><a href="quotation_request.php">Raised decking</a></li>
    <li><a href="quotation_request.php">Grass seeding and plant selections</a></li>
    <li><a href="quotation_request.php">Garden clearances</a></li>
    <li><a href="quotation_request.php">Retaining wall systems</a></li>
     
    </ul>
    I also have a php file with a sendmail form:

    Code:
    <?php
    
    // process the email
    if (array_key_exists('submit', $_POST)) {
      ini_set("sendmail_from", "info@test.com");
      $to = 'john@test.com'; // use your own email address
      $subject = 'Quotation request from website!';
      
      // list expected fields
      $expected = array('turfselectbox', 'turfarea', 'radioturf', 'radiodel', 'serviceselectbox', 'name', 'postaddress', 'pcode', 'tel', 'email','notes');
      // set required fields
      $required = array('turfselectbox', 'serviceselectbox', 'name', 'postaddress', 'pcode', 'tel', 'email');
      // create empty array for any missing fields
      $missing = array();
      
      // process the $_POST variables
      foreach ($_POST as $key => $value) {
        // assign to temporary variable and strip whitespace if not an array
        $temp = is_array($value) ? $value : trim($value);
    	// if empty and required, add to $missing array
    	if (empty($temp) && in_array($key, $required)) {
    	  array_push($missing, $key);
    	  }
    	// otherwise, assign to a variable of the same name as $key
    	elseif (in_array($key, $expected)) {
    	  ${$key} = $temp;
    	  }
    	}
    
    	// validate the email address
    	if (!empty($email)) {
    
    	// regex to ensure no illegal characters in email address
    	$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
    
    	// reject the email address if it doesn't match
    	if (!preg_match($checkEmail, $email)) {
    	array_push($missing, 'email');
    	}
    	}
     
      // go ahead only if all required fields OK
      if (empty($missing)) {
        // build the message
        $message = "Selected turf required: $turfselectbox\n\n";
        $message .= "Area to apply turf: $turfarea\n\n";
    	$message .= "Soil required: $radioturf\n\n";
    	$message .= "Delivery required: $radiodel\n\n";
        $message .= "Service required: $serviceselectbox\n\n";
        $message .= "Name/Business: $name\n\n";
    	$message .= "Postal address: $postaddress\n\n";
    	$message .= "Post code: $pcode\n\n";
    	$message .= "Telephone no.: $tel\n\n";
    	$message .= "Email address: $email\n\n";
    	$message .= "Message: $notes\n\n";
    
        // limit line length to 70 characters
        $message = wordwrap($message, 200);
      
        // send it  
        $sendmail_from = mail($to, $subject, $message);
    	if ($sendmail_from) {
    	  // $missing is no longer needed if the email is sent, so unset it
    	  unset($missing);
    	  }
        }
      }
    ?>
        <?php
    		if ($_POST && isset($missing)) {
    ?>
        <p class="main_warning">Please complete the missing item(s) indicated. Your message has not been sent.</p>
        <?php
    		  }
    		elseif ($_POST && !$sendmail_from) {
    ?>
        <p class="main_warning">Sorry, there was a problem sending your message. Please try later.</p>
        <?php
    		  }
    		elseif ($_POST && $sendmail_from) {
    ?>
        <span class="confmsg"><h2>Thank you for your quotation request. We will reply to you as soon as possible.</h2></span>
    
    <?php } ?>
    
    <form id="quoteform" name="contactform" method="post" action="">
    
          <?php
    	if (isset($missing) && in_array('turfselectbox', $missing)) { ?>
          <span class="warning">Enter turf if required.</span>
          <?php } ?>
    
    <label><abbr title="Select turf required."><font color="#FF0000" size="1"><sup>*</sup></font>Select turf required:</abbr>
    
    <select name="turfselectbox" id="turfselectbox" size="1">
    <option selected="selected">Please Select</option>
    <option>Premium turf</option>
    <option>None</option>
    </select>
    </label>
    
    <label>
    <abbr title="Area to apply turf:">Area to apply turf:</abbr>
    <input type="text" name="turfarea" id="turfarea" maxlength="40"/> M<sup>2</sup>
    <a href="javascript:popUp('turf_calculator.php')"><img src="img/calculator.jpg" width="16" height="16" title="Turf Calculator" alt="Turf Calculator" /></a></label>
    
    
    <label><abbr title="Do you require soil?">Do you require soil?</abbr></label>
    
    <label><input id="radiosoil" name="radioturf" type="radio" value="Yes" checked="checked"></input>Yes</label>
    
    <label><input id="radiosoilb" name="radioturf" type="radio" value="No"></input>No</label>
    
    <label><abbr title="Do you require delivery?">Do you require delivery?</abbr></label>
    
    <label><input id="radiodel" name="radiodel" type="radio" value="Yes" checked="checked"></input>Yes</label>
    
    <label><input id="radiodelb" name="radiodel" type="radio" value="No"></input>No</label>
    
          <?php
    	if (isset($missing) && in_array('serviceselectbox', $missing)) { ?>
          <span class="warning">Enter the type of service you require.</span>
          <?php } ?>
    
    <label>
    
    <abbr title="What type of service do you require?"><font color="#FF0000" size="1"><sup>*</sup></font>What service do you require?</abbr>
    
    //Service select box relates to the ul menu.
    
    <select name="serviceselectbox" id="serviceselectbox" size="1">
    <option selected="selected">Please select</option>
    <option>Turf supplied to order</option>
    <option>Lawn preparation and turf laying</option>
    <option>Stonewalling</option>
    <option>Brick-paved driveways</option>
    <option>Hedge laying</option>
    <option>Garden planning</option>
    <option>Garden sheds and benches</option>
    <option>Timber fences</option>
    <option>Rockery creation</option>
    <option>Water features and ponds</option>
    <option>Raised decking</option>
    <option>Grass seeding and plant selections</option>
    <option>Garden clearances</option>
    <option>Retaining wall systems</option>
    <option>None</option>
    </select>
    
    </label>
    
          <?php
    	if (isset($missing) && in_array('name', $missing)) { ?>
          <span class="warning">Enter your name.</span>
          <?php } ?>
    
    <label><abbr title="Enter your name."><font color="#FF0000" size="1"><sup>*</sup></font>Name/Business:</abbr>
    
    <input type="text" name="name" id="name" maxlength="35" /></label>
    
          <?php
    	if (isset($missing) && in_array('postaddress', $missing)) { ?>
          <span class="warning">Enter your postal address.</span>
          <?php } ?>
    
    <label><abbr title="Enter your postal address."><font color="#FF0000" size="1"><sup>*</sup></font>Postal address:</abbr></label>
    
    <label><textarea name="postaddress" id="postaddress" cols="0" rows="7"></textarea></label>
    
          <?php
    	if (isset($missing) && in_array('pcode', $missing)) { ?>
          <span class="warning">Enter your postal code.</span>
          <?php } ?>
    
    <label>
    <abbr title="Enter your post code."><font color="#FF0000" size="1"><sup>*</sup></font>Post code:</abbr> 
    <input type="text" name="pcode" id="pcode" maxlength="25" /></label>
    
          <?php
    	if (isset($missing) && in_array('tel', $missing)) { ?>
          <span class="warning">Enter your contact telephone no.</span>
          <?php } ?>
    
    <label>
    <abbr title="Enter your contact telephone no."><font color="#FF0000" size="1"><sup>*</sup></font>Telephone no.:</abbr> 
    <input type="text" name="tel" id="tel" maxlength="25" /></label>
    
          <?php
    	if (isset($missing) && in_array('email', $missing)) { ?>
          <span class="warning">Enter your email address.</span>
          <?php } ?>
    
    <label>
    <abbr title="Enter your email address."><font color="#FF0000" size="1"><sup>*</sup></font>Email address:</abbr>
    
    <input type="text" name="email" id="email" maxlength="25" /></label>
    
    <label><abbr title="Enter your message.">Message:</abbr></label>
    
    <label><textarea name="notes" id="notes" cols="0" rows="7"></textarea></label>
    
    <input type="submit" name="submit" id="submit" class="submit" value="Request Quotation" title="Request Quotation" />
    
    <input type="reset" name="reset" id="reset" class="reset" value="Reset" title="Reset" />
    
    </form>
    What I would like to try and achieve is when the user selects a list item from the included file, I would like the link to go to the php file with the form, and from the option select box, php selects the appropriate option from the list that relates from the list item.

    I hope it makes sense on what Iím trying to do.

    Any help or suggestions greatly appreciated.

    Many thanks, John.
    Last edited by john0611; 07-24-2009 at 02:03 PM.

  2. #2
    Join Date
    Jun 2008
    Posts
    40
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    How I handle situations like this is...

    I create a PHP array with my link URLs and the link text. Then I have a PHP script go through the array and echo out the links as necessary. In the processing, store a variable that references what link you save. It can be the array key or the link text. Then just throw an if statement in there to echo a "selected="selected"".

    Code:
    $urls[] = array('linkURL1', 'linkName1');
    $urls[] = array('linkURL2', 'linkName2');
    $urls[] = array('linkURL3', 'linkName3');
    $urls[] = array('linkURL4', 'linkName4');
    
    foreach ($urls as $key => $linkSet) {
        echo '<a href="'.$linkSet[0].'"'.(($_SESSION['selectedLink'] == $key)?' selected="selected":'').'>'.$linkSet[1].'</a>';
    }

  3. The Following User Says Thank You to Keleth For This Useful Post:

    john0611 (07-24-2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Hi Keleth,

    I see that looks like the solution, when trying to implement the php:

    PHP Code:
    foreach ($urls as $key => $linkSet) {
        echo 

    '<a href="'.$linkSet[0].'"'.(($_SESSION['selectedLink'] == $key)?' selected="selected":'').'>'.$linkSet[1].'</a>';

    I get this error: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\wamp\www\baturf\quotation_request.php on line 166

    Any ideas I have tried to correct, but result ending up with more errors.

    Thanks, John.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •