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 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效