Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I'm building a website where are zip code are important. Based on the zip code the contact form is redirected to an email adress. So I live in Holland where we have 12 provinces, and based on the zip code you can know where somebody lives, lets say you has zip code: 5233HS You live in Noord-Brabant. Then it sends the email to somebody there. But if you got a zip code in another province, the mail is forwarded to another email adress in that province. I was thinking about a Javascript which compare all zip code's until it found one that's equal. Or is there a solution simpeler?

And how to do it with javascript or another method.
I done something in php but it won't work.

PHP
 if (isset($_POST['submit'])) {

 function random_strings($length_of_string)
 {

     $str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

     return substr(str_shuffle($str_result), 0, $length_of_string);
 }

 $Output = random_strings(10);

 $conn = mysqli_connect("localhost", "...", "...", "...");

 if($conn === false){
     die("ERROR: Could not connect. " . mysqli_connect_error());
 }

 $Zip = $_POST['Zip'];
 $name = $_POST['Name'];
 $Email = $_POST['Email'];
 $Phone = $_POST['Phone'];
 $Subject = $_POST['Problem'];

 //All variables now defined, you can now reference them all...
 $Zip = $_POST['Zip'];
  $Subject = $_POST['Problem'];

  $txt1 = "Noord-Holland";
  $arr1 = range(1000,1299); //Noord-Holland
  $txt2 = "Flevoland";
  $arr2 = range(1300,1379); //Flevoland

if ($arr1 == $Zip) {


  $mailTo = "info@probleeminhuis.nl";
  mail($mailTo, $txt1, $Subject, $Zip);
}
elseif ($arr2 == $Zip) {


  $mailTo = "info@probleeminhuis.nl";
  mail($mailTo, $txt2, $Subject, $Zip);
}


 if ($stmt = mysqli_prepare($conn, "INSERT INTO ID (Id, Name, Email, Phone, Problem, Zip) VALUES (?, ?, ?, ?, ?, ?)")) {
     mysqli_stmt_bind_param($stmt, "ssssss", $Output, $name, $Email, $Phone, $Subject, $Zip);
     if (mysqli_stmt_execute($stmt)) {
       echo ' ';
       echo "<div id=\"PHP-output\"> Succesvol Verstuurd </div>";
     } else{
         echo ' ';
         echo "<div id=\"PHP-output\"> Sorry, Het kon niet worden verstuurd, probeer het later nog eens of neem contact op met de klantenservice. $sql.  </div>" . mysqli_error($conn);
     }
 }

 mysqli_close($conn);


And the origin of the variables:

HTML
<pre>            <form action="../../PHP/Ticketnumber.php" method="POST">
            <input style="width: 90%;" type="text" name="Name" placeholder="Volledige naam">
            <input style="width: 90%;" type="text" name="Email" placeholder="E-mail">
            <input style="width: 90%;" type="text" name="Phone" placeholder="Telefoon">
            <input style="width: 90%;" type="text" name="Zip" placeholder="Postcode">
            <input style="width: 90%;" type="text" name="Problem" placeholder="Omschrijf je probleem zo simpel mogelijk">
            <button style="margin-bottom: 10px" class="button-probleem" type="submit" name="submit">Verzenden</button>
            <p>Als je op verzenden drukt ga je akkoord met de <a style="text-decoration: underline; cursor: pointer;" href="../../Algemene-voorwaarden.html">algemene voorwaarden!</a></p>
            </form>


It don't send an email.

Improved code where elseif is not working:
PHP
<pre> 
  $name = $_POST['Name'];
  $Email = $_POST['Email'];
  $Phone = $_POST['Phone'];
  $Subject = $_POST['Problem'];
  $Zip = $_POST['Zip'];


if ($Zip > 999 && $Zip < 1300) {

   $Zip = $_POST['Zip'];
   $txt1 = "Noord-Holland";

   $mailTo = "";
   mail($mailTo, $txt1, $Subject, $Zip);
 }
 elseif ($Zip > 1299 && $Zip < 1380) {

     $txt2 = "Flevoland";
     $Zip = $_POST['Zip'];

     $mailTo = "";
     mail($mailTo, $txt2, $Subject, $Zip);
 }


Thanks, Sam

What I have tried:

I tried to code in PHP. Ans searched google.
Posted
Updated 3-Jul-20 0:48am
v7
Comments
MadMyche 2-Jul-20 17:14pm    
And if you have a 5-digit postal code (eg 99999) you could be in 1 of 76 different countries; one of which is the United States, and they have over 80,000 postal codes in use.
Point of the story: I would look at the province/country portions of the form to decide where to send the email.

Me? I'd use a database and let it handle it - probably from the server, rather than the client. Doing it in JS is a little "silly" - because you have to load the page HTML with both the JS to check, and a table of codes for the JS to work from, then get the JS to send an email - via the server - to someone in the appropriate province. And it's got to be more efficient to do all of that in the server directly, as well as making it much simpler!

The other alternative is to use a "decode" site (I found this in seconds: Postal Codes rotterdam[^] but there are probably better ones or even "query based" ones provided by your postal service taht Google can find for you locally) and let it worry about updates.

I'm assuming that the "locality" or the email sending is to allocate it to the appropriate sales team or similar? If so, then a DB is definitely the best idea: people leave, new offices open, and so forth - and somebody has to update destinations quickly and easily, which is difficult with a website update.
 
Share this answer
 
I agree with OriginalGriff, there are easier ways but based on your code, it will not run as you reference $Zip and $arr before it is defined, change this code -
PHP
if ($arr == $Zip) {

$arr = range(1000,1299); //Noord-Holland

  $Zip = $_POST['Zip'];
  $Subject = $_POST['Problem'];
  $txt = "Noord-Holland";

$mailTo = "";
mail($mailTo, $txt, $Subject, $Zip);
}

to this -
PHP
  $Zip = $_POST['Zip'];
  $Subject = $_POST['Problem'];
  $txt = "Noord-Holland";
  $arr = range(1000,1299); //Noord-Holland

//All variables now defined, you can now reference them all...
if ($arr == $Zip) {
  $mailTo = "";
  mail($mailTo, $txt, $Subject, $Zip);
}
 
Share this answer
 
v3
Comments
Sam Vorst 3-Jul-20 3:50am    
Thanks, but one problem. If I paste your code it won't mail, yes I filled in the mailadress.. It mails only if I paste the variables inside the if. But that is not good because I need to have else/elseif statement too. I edited the code in my question. Including teh elseif.
Andre Oosthuizen 3-Jul-20 5:38am    
To understand what you need to do, I need a lot more code like where is your variables coming from, what happens before sending mail, during sending mail and after sending mail.

Did you look into PHPMailer as per a previous post of yours?
Sam Vorst 3-Jul-20 5:39am    
My variables is coming from a contact form I will update it in the question.
Yeah but don't understand it really.
Andre Oosthuizen 3-Jul-20 5:54am    
Also have a look at this page, might help you out - https://www.codeproject.com/Articles/1176482/Send-HTML-mail-with-attachments-using-PHP
Sam Vorst 3-Jul-20 5:57am    
Thanks looks difficult, but do you know what the issue is with my php code??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900