0

I'm trying to retrieve the value of a radio module through PHP but the variable doesn't seem to pick up the value.

The "showhidediv" and "hidediv" are functions from scripts for showing and hiding fields depending on what radio button you press. Shouldn't be a reason I'm experiencing this issue.

I'm very new to coding, so please keep it simple. Thank you for your help!

Don't mind the xxx_status variables. I just used them to try and find errors. The else echo thing pops up at the moment, which I guess tells me that rGroup doesn't pick up the value "direkt" from the HTML to be printed as the rGroup variable. I want the $rGroup variable to print the value from the radio. At the moment, it does not pick up "direkt", "senare" or "maila". At the moment the email, in which I'm recieving the filled out contact form, shows "When is empty", which means my rGroup variable has no value, right?

Other variables are working fine. I have submitted the date variable which is working perfectly for you to see.

All the way down i also submitted the JS in which i have not put in the variables, but that shouldn't matter right? I think that's just some sort of submission check.

Many thanks.

/* The HTML */

<div class="radios">
<input type="radio" name="rgroup" value="direkt" id="1" 
onclick="hidediv('time'); hidediv('date'); hidediv('date-text'); 
hidediv('time-text');"/>
<label class="radio" for="1">Direkt</label>

<input type="radio" name="rgroup" value="senare" id="2" 
onclick="showhidediv('time'); showhidediv('date'); showhidediv('date-text'); showhidediv('time-text');"/>
<label class="radio" for="2">Senare:</label>

<input type="radio" name="rgroup" value="maila" id="3" 
onclick="hidediv('time'); hidediv('date'); hidediv('date-text'); 
hidediv('time-text');"/>
<label class="radio" for="3">Maila mig istället</label>
        </div>

<div class="form-group">
            <div class="controls" id="date-text" style=display:none >
                <h6>Datum:</h6>
              <input type="date" value="2019-mm-dd" class="form-control" id="date"  required data-error="Ange ett datum">
              <div class="help-block with-errors"></div>
            </div>


/* The PHP */

// När ska vi höra av oss
    $rGroup =$_POST["rGroup"];

    $direkt_status ="unchecked";
    $senare_status ="unchecked";
    $maila_status ="unchecked";

    $radio_var= ($_POST["rGroup"]);

    if ($rGroup =="direkt"){
        $direkt_status="checked";
    }
    else {
        echo "Incorrect";
    }

    // DATE
    if (isset($_POST["date"])) {
        $date = $_POST["date"];
    } else {
        $errorMSG = "Ange datum ";
    }

// prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $name;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $email;
    $Body .= "\n"; 
    $Body .= "Phone: ";
    $Body .= $phone;
    $Body .= "\n";
    $Body .= "When: ";
    $Body .= $rGroup = $rGroup ? $rGroup:"When is empty";
    $Body .= "\n";
    $Body .= "När ska vi höra av oss: ";
    $Body .= $direkt_status = $direkt_status ? $direkt_status:"Radio is empty";


  /* The JS */
function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#name").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var date = $("#date").val();
    var time = $("#time").val();
    var msg_subject = $("#msg_subject").val();
    var message = $("#message").val();
    var rgroup = $(":radio[name=rgroup]:checked").val();

    $.ajax({
        method: "POST",
        url: ajcf.ajaxurl, 
        data:  {
            action: 'ajcf_sentemail',
            name: name,
            email: email,
            phone: phone,
            date: date,
            time: time,
            msg_subject: msg_subject,
            message: message,
            rgroup: rgroup
        }, 
        success : function(text){
            if (text == "success"){
                formSuccess();
            } else {
                formError();
                submitMSG(false,text);
            }
        }
    });
}
Fred B
  • 29
  • 8

1 Answers1

0

You're not sending the value of the selected radio button in the AJAX call. Use $(":radio[name=rGroup]:checked").val() to get this.

function submitForm() {
  // Initiate Variables With Form Content
  var name = $("#name").val();
  var email = $("#email").val();
  var phone = $("#phone").val();
  var date = $("#date").val();
  var time = $("#time").val();
  var msg_subject = $("#msg_subject").val();
  var message = $("#message").val();
  var rgroup = $(":radio[name=rgroup]:checked").val();

  $.ajax({
    method: "POST",
    url: ajcf.ajaxurl,
    data: {
      action: 'ajcf_sentemail',
      name: name,
      email: email,
      phone: phone,
      date: date,
      time: time,
      msg_subject: msg_subject,
      message: message,
      rGroup: rgroup
    },
    success: function(text) {
      if (text == "success") {
        formSuccess();
      } else {
        formError();
        submitMSG(false, text);
      }
    }
  });
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thank you very much, I'm sure that has to go in there! However, I'm still not getting the value to show. I'll update the code so you can see my new version. – Fred B Jul 17 '19 at 22:03
  • I had incorrect capitalization in `name=rGroup`. Try it now. – Barmar Jul 17 '19 at 22:07
  • Woho it works!! I realized the capital G was a bad idea so I went back and changed it all. Very kind of you to take the time and help. Much appreciated. – Fred B Jul 17 '19 at 22:17