duan2891 2019-07-17 21:48
浏览 82
已采纳

PHP无法从无线电输入中检索值[重复]

This question already has an answer here:

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 .= "
";
    $Body .= "Email: ";
    $Body .= $email;
    $Body .= "
"; 
    $Body .= "Phone: ";
    $Body .= $phone;
    $Body .= "
";
    $Body .= "When: ";
    $Body .= $rGroup = $rGroup ? $rGroup:"When is empty";
    $Body .= "
";
    $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);
            }
        }
    });
}
</div>
  • 写回答

1条回答 默认 最新

  • douma5954 2019-07-17 21:53
    关注

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

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因