PHP无法从无线电输入中检索值

时间:2019-07-17 21:48:22

标签: php jquery html

我正在尝试通过PHP检索单选模块的值,但是该变量似乎未获取该值。

“ showhidediv”和“ hidediv”是脚本中的函数,用于根据您按下的单选按钮来显示和隐藏字段。这不是我遇到此问题的原因。

我对编码非常陌生,所以请保持简单。谢谢您的帮助!

不要介意xxx_status变量。我只是用它们来尝试发现错误。此时会弹出else echo东西,我想这告诉我rGroup不会从HTML中获取要打印为rGroup变量的值“ direkt”。我希望$ rGroup变量从收音机中打印值。目前,它没有拾取“ direkt”,“ senare”或“ maila”。目前,我正在接收已填写的联系表的电子邮件中显示“何时为空”,这意味着我的rGroup变量没有值,对吗?

其他变量工作正常。我已经提交了date变量,它非常适合您查看。

一直以来,我还提交了JS,但我没有在其中放入变量,但这没关系吧?我认为那只是某种形式的提交检查。

非常感谢。

/* 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);
            }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

您不会在AJAX调用中发送所选单选按钮的值。使用$(":radio[name=rGroup]:checked").val()来获取。

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);
      }
    }
  });
}