Jump to content

FormToEmail list/menu true/false


PrPrO

Recommended Posts

Hello..

 

My problem is that I have created the form and formtoemail.php and everything works fine and now I created a list to choose the product...

Here is the list:

 <select name="select" id="select">
                                   <option value="Molimo odaberite" selected="selected">Molimo odaberite</option>
                                   <option value="Trup #T100">Trup #T100</option>
                                   <option value="Trup #T101">Trup #T101</option>
                                   <option value="Trup #T102">Trup #T102</option>
                                   <option value="Trup #T103">Trup #T103</option>
                                   <option value="Trup #T104">Trup #T104</option>
                                   <option value="Trup #T105">Trup #T105</option>
                                   <option value="Trup #T106">Trup #T106</option>
                                   <option value="Trup #T107">Trup #T107</option>
                                   <option value="Trup #T108">Trup #T108</option>
                                   <option value="Trup #T109">Trup #T109</option>
                                   <option value="Trup #T110">Trup #T110</option>
                                   <option value="Trup #T111">Trup #T111</option>
                                   <option value="Trup #T112">Trup #T112</option>
                                   <option value="Trup #T113">Trup #T113</option>
                                   <option value="Trup #T114">Trup #T114</option>
                </select>

 

And the current FormToEmail.php code

 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "info@clm-pro-tk";
    $email_subject = "Your email subject line";
     
     
    function died($error) {
        // your error code can go here
        echo "Ispričavano se, prilikom po?iljanja va?eg upita smo prona?li pogre?ku/e. ";
        echo "Pogre?ka/e će biti prikazane ispod.<br /><br />";
        echo $error."<br /><br />";
        echo "Vrni se i popravi.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['ime']) ||
        !isset($_POST['email']) ||
        !isset($_POST['naslov']) ||
        !isset($_POST['poruka'])) {
        died('Ispričavamo se, prona?li smo npogre?ku prilikom slanja va?eg upita.');      
    }
     
    $ime = $_POST['ime']; // required
    $email = $_POST['email']; // required
    $naslov = $_POST['naslov']; // required
    $poruka = $_POST['poruka']; // required
$trup = $_POST['select'] ; // not required
    $newsletter = $_POST['newsletter']; // not required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email)) {
    $error_message .= 'E-mail ne valja.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$ime)) {
    $error_message .= 'Ime ne valja.<br />';

}
  if(strlen($naslov) < 2) {
    $error_message .= 'Naslov ne valja.<br />';

  }
  if(strlen($poruka) < 2) {
    $error_message .= 'Poruka ne valja.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "Ime: ".clean_string($ime)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Naslov: ".clean_string($naslov)."\n";
    $email_message .= "Poruka: ".clean_string($poruka)."\n";
     
     
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
@ mail("info@clm-pro.tk", $naslov,
  $poruka, "From:" . $email  . "\r\n" . $newsletter . "\r\n" . $trup . "\r\n" . $ime); 
  
   //AUTO RESPONCE MESSAGE
//Create main headers
   $pporuka = "";
   $pporuka .= "Po?tovani ";
   $pporuka .= $ime;
   $pporuka .= ", hvala na vasem upitu u vezi proizvoda: ";
   $pporuka .= $trup;
   $pporuka .= ", javit cemo vas se u ?to kracem roku. ";
   $pporuka .= "
Upitali ste nas slijedece: ";
   $pporuka .= $poruka;
//E-mails subject
//Send the email
   mail($email, $naslov, $pporuka, "From:" . "info@clm-pro.tk");
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

 

The error messages are on Croatian but you don't have to bother with that..

The problem is that the default selected item is Please choose(translated to english) and I need in the formtoemail to say if the select(that list) is "Molim odaberite" --> please choose then send an error message like for other parameters.. and if it is Trup #100 - Trup #114 then do nothing and submit form

 

I think there is a simple msolution but I can't remember any -.-

 

Thank you in advance!

Link to comment
Share on other sites

I fixed my problem by adding the

 if($select == 'Molimo odaberite'){
died('Ispričavamo se, prona?li smo pogre?ku prilikom slanja va?eg upita.');
    }

Because I just want to send an error if "Molimo odaberite" is chosen, otherwise to send the porduct name in the email form

 

Thank you fro you help!

Link to comment
Share on other sites

Molim te, no problem  ;)

This way will work, but is not very effective. If someone type the value which name does not exist in options, the result could be true.

My suggestion is creating an array with all available options.

Take a look for seconds what I mean, for sure you can add more securities issues, if you want it.

// array items
$validList = array('Trup #T100','Trup #T101','Trup #T102','Trup #T103','Trup #T104','Trup #T105');
// function, check for correct items exist in array
function isValidList($str){
return (isset($_POST['select']) && $str != 'Molimo odaberite') ? true : false;}

if(isset ($_POST['Submit'])){
var_dump(isValidList($_POST['select']));
}

// html form 


<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <select name="select">
        <option value="Molimo odaberite" selected="selected">Molimo odaberite</option>
        <?php foreach ($validList as $val): ?>
        <option value="<?php echo $val; ?>"><?php echo $val; ?></option>   
         <?php endforeach;?>
    </select>
    <input type="submit" name="Submit" />
</form>

Link to comment
Share on other sites

Little mistake, this form should be return true:

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <select name="select">
        <option value="Molimo odaberite" selected="selected">Molimo odaberite</option>
        <?php foreach ($validList as $val): ?>
        <option value="<?php echo $val; ?>"><?php echo $val; ?></option>   
         <?php endforeach;?>
         <option value="Trup #T106">Trup #T106</option>
    </select>
    <input type="submit" name="Submit" />
</form>

 

Try,

 

// array items
$validList = array('Trup #T100','Trup #T101','Trup #T102','Trup #T103','Trup #T104','Trup #T105');

// function, check for correct items exist in array
function isValidList($str){
    global $validList;
    return (isset($_POST['select']) && in_array($str, $validList)) ? true : false;}

if(isset ($_POST['Submit'])){
var_dump(isValidList($_POST['select']));
}

?>


<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <select name="select">
        <option value="Molimo odaberite" selected="selected">Molimo odaberite</option>
        <?php foreach ($validList as $val): ?>
        <option value="<?php echo $val; ?>"><?php echo $val; ?></option>   
         <?php endforeach;?>
         <option value="Trup #T106">Trup #T106</option>
    </select>
    <input type="submit" name="Submit" />
</form>

 

Now, as you can see, every value out of array named validList,  should be false.

Link to comment
Share on other sites

That's one dead kitten, Jazzman1. :(

 

Section 11, btw.

@Christian, there are many ways to solve this issue. I don't pretend at all that my code is a perfect. There is no a perfect code, also no perfect programmers.

The whole idea is to show some beginning programers how could be done the same task in different way, that's why I write in this forum.

By the way, you are absolutely right about global, but the answer really depends. In some cases you don't have a choice  ;)

 

Link to comment
Share on other sites

function isValidList($str, $validList) {
}

var_dump(isValidList($_POST['select'], $validLIst));

Much better solution, with no hidden dependencies in the code. Nothing that'll come and bite an inexperienced coder in the arse, later on when s/he does some changes.

 

While it is true that there are many methods to accomplish something on, and that there are no real perfect ways to do it, it is still important to teach the Right Way(s) to do something. That way we are ensuring that we help to actually improve the quality of the code being produced, and whenever the programmer in question becomes skilled enough to learn about other ways s/he will also be skilled enough to know the difference between the Bad and the Good Stuff. (Hopefully. :P)

 

PS: If you don't have a choice but to use global, then you need to redesign your code. I haven't used "global" for the last 10 years, and I've had no problems with it. ;)

Link to comment
Share on other sites

First of all, I have to admit that your expressions about the answers given from you, there are very impressive - I like this.

I've seen lots of English people in this forum, which don't express themselves very well in a proper English.

The computer's language is named English as medicine's language named Latin.

That's why, all computer languages named computer languages, not for example computer typing.

 

Secondly, that's the power of the forum, me or someone wrote something wrong and others members correct or give him a better solution.

 

PS: If you don't have a choice but to use global, then you need to redesign your code. I haven't used "global" for the last 10 years, and I've had no problems with it

 

I've supported one site wrote in PHP 4.3, there are lots of old-school server side scripts inside of it. If I have to redesign it, i need to write everything from scratch  :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.