Send PDF link in email based on radio button selection

I am using custom email and form in my wordpress site.

Thing is in form there are 2 radio buttons :

  1. English
  2. Chinese

Other fields are normal.

I have 2 PDF uploaded to server for both above languages. What I want is to send use PDF link in email based on language selection in form.

I am trying but it is sending always same PDF link.

Code to check data and send in email :

if(isset($_POST['submit']) && isset($_POST['language1'])){
  $clname=$_POST['clname'];
  $clsurname=$_POST['clsurname'];
  $clemail=$_POST['clemail'];
  $radio = $_POST['language1'];
  $fileatt1     = "http://example.com/english.pdf";
  $fileatttype = "application/pdf";
  $fileattname = "newname.pdf"; //name that you want to use to send or you can use the same name

  $headers="From: ". $clname .' <'. $clemail .'>' . "\r\n";
  $mail = get_option('admin_email');
  $subject = "Testing";


  $message="Name:".$clname.' '.$clsurname.'\n\n
    Email:'.$clemail.'\n\n Radiovalue:'.$radio.'\n'. $fileatt;

  wp_mail($mail,$subject,$message,$headers);
            $emailsent= true;
}

Form Code

<div class="overflow-hidden">
  <form class="text-center" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <div class="col-md-6">
       <p class="tns">First Name *:</p>
       <input type="text" name="clname" placeholder="First Name" required />
    </div>
    <div class="col-md-6">
      <p class="tns">Last Name *:</p>
        <input type="text" name="clsurname" placeholder="Last Name" required />
        <input type="radio" name="language1" value="English">English 
        <input type="radio" name="language2" value="Chinese">Chinese </div>

      </div>

      <hr>
      </div>
      <div class="overflow-hidden">
      <div class="col-sm-6 col-sm-offset-3">


      <input type="submit" name="submit" value="submit" />
    </div>
  </div>
</form>

I have tried to put another PDF link in ELSE condition but that is not working. I have also tried by put 2 different if conditions but that is also not working for me.

1 Answer
1

You should use the same name attribute for your 2 radio inputs. So let’s say that name is language. If language is set, you have 2 options represented by value, either English or Chinese.

In the code you posted, that is mainly your problem. You set your $radio variable to be $_POST['language1'] but $_POST['language1'] will ALWAYS be English because the Chinese value is set by $_POST['language2']

in your form markup, change your radio input name values

<div class="overflow-hidden">
  <form class="text-center" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <div class="col-md-6">
      <p class="tns">First Name *:</p>
      <input type="text" name="clname" placeholder="First Name" required />
    </div>
    <div class="col-md-6">
      <p class="tns">Last Name *:</p>
      <input type="text" name="clsurname" placeholder="Last Name" required />
      <input type="radio" name="language" value="English">English 
      <input type="radio" name="language" value="Chinese">Chinese
    </div>

    <hr>

    <div class="overflow-hidden">
      <div class="col-sm-6 col-sm-offset-3">
        <input type="submit" name="submit" value="submit" />
      </div>
    </div>
  </form>
</div>

In your PHP:

  • Change references of language1 to language
  • Use ternary operator to set $fileatt. If $radio is English output English url to file. Output Chinese url otherwise.

.

if(isset($_POST['submit']) && isset($_POST['language'])){

  $radio       = $_POST['language'];
  $fileatt="English" == $radio ? "http://example.com/english.pdf" : "http://example.com/chinese.pdf" ;
  $fileatttype = "application/pdf";


  $headers="From:" . $_POST['clname'] . ' <'. $_POST['clemail'] .'>' . "\r\n";
  $mail = get_option('admin_email');
  $subject = "Testing";


  $message="Name: " . $_POST['clname'] . ' ' . $_POST['clsurname'] .'\n\n
  Email:'.$_POST['clemail'].'\n\n Radiovalue:' . $radio . '\n'. $fileatt;

  wp_mail($mail,$subject,$message,$headers);
  $emailsent= true;
}

Leave a Comment