简体   繁体   English

如何使用AJAX向php提交隐藏值

[英]How can i submit hidden values to php using AJAX

i am trying to send some values from html form to php page. 我试图从html表单发送一些值到PHP页面。 here in the form i have some values which are hidden.and one text field which its value has to be passed to php page. 在这里我有一些隐藏的值和一个文本字段,其值必须传递给PHP页面。

index.php: index.php文件:

<?php
      if ($login->isUserLoggedIn() == true){    
      ?>
            <div class="panel panel-default">
            <div class="panel-heading"><h4>Invite friend</h4></div>
            <div class="panel-body">                        
            <form action="friend-invite.php" method="get">              
                <div class="col-md-4 col-lg-4">
                  <label class="control-label" for="friend">Enter email address</label>              
                  <input type="email" class="form-control" name="friendemail" id="friendemail" placeholder="sam@uncle.com" required><br>                                
                  <?php 
                  echo '<input type="hidden" name="invitename" value="'.$_SESSION["user_name"].'">' ;                 
                  echo '<input type="hidden" name="invite-url" value="'.$_SERVER['REQUEST_URI'].'">';
                  echo '<input type="hidden" class="invite-product" name="invite-product-name">';
                  ?>
                  <input type="submit" name="submit" value="Invite" class="btn btn-primary">
                </div>            
            </form>
            <div class="mail-message"></div>
            </div>
            </div>
      <?php 
      }else{
      }?>

friend-invite.php: 朋友,invite.php:

<?php
    include('_header.php');
    $user_email = $_GET['friendemail'];      
    $invited_by = $_GET['invitename'];  
    $invite_link = $_GET['invite-url'];    
    $product_name = $_GET['invite-product-name'];    
    if (isset($user_email, $invited_by, $invite_link, $product_name)){
        sendInvitation($user_email,$invited_by,$invite_link,$product_name);        
    } else {  
        echo "Are you trying to do something nasty??";
    }

    function sendInvitation($user_email,$invited_by,$invite_link,$product_name)
    {
        $mail = new PHPMailer;
        if (EMAIL_USE_SMTP) {
            $mail->IsSMTP();
            $mail->SMTPAuth = EMAIL_SMTP_AUTH;
            if (defined(EMAIL_SMTP_ENCRYPTION)) {
                $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
            }
            $mail->Host = EMAIL_SMTP_HOST;
            $mail->Username = EMAIL_SMTP_USERNAME;
            $mail->Password = EMAIL_SMTP_PASSWORD;
            $mail->Port = EMAIL_SMTP_PORT;
            $mail->IsHTML(true);
        } else {
            $mail->IsMail();
        }
        $mail->From = EMAIL_VERIFICATION_FROM;
        $mail->FromName = $invited_by;
        $mail->AddAddress($user_email);
        $mail->Subject = SHOP_INVITE;
        $link = $invite_link;
        $mail->Body = $invited_by." ".FRIEND_INVITE_PRODUCT."<a href='".$link."'>".$product_name."</a>";        
        if(!$mail->Send()) {
            $this->errors[] = MESSAGE_VERIFICATION_MAIL_NOT_SENT . $mail->ErrorInfo;
            return false;            
        } else {        
            return true;            
        }
    }
?>

AJAX function: AJAX功能:

$(function () {
        $('form').on('submit', function (e) {
          e.preventDefault();
          $.ajax({
            type: 'get',
            url: 'invite-friend.php',
            data: $('form').serialize(),
            success: function () {
                $(".mail-message").html('<div class="alert alert-success"><strong>Success!</strong> Indicates a successful or positive action.</div>');                
            }
        });
    });
});

the friend-invite.php page is getting the values that has been passed to it and check if the values has been set, if it has been set then it will call the php function sendInvitation() with the parameters. friend-invite.php页面获取已传递给它的值并检查是否已设置值,如果已设置,则它将使用参数调用php函数sendInvitation()。 now all these things are happening pretty good.but i want to do it through AJAX. 现在所有这些事情都发生得很好。但我想通过AJAX来做。 how can i do that. 我怎样才能做到这一点。

You forgot to give your hidden fields an id: 你忘了给隐藏的字段一个id:

 echo '<input type="hidden" name="invitename" id="invitename" value="'.$_SESSION["user_name"].'">' ;

... ...

Change your index.php as: 将index.php更改为:

<?php
  if ($login->isUserLoggedIn() == true){    
  ?>
        <div class="panel panel-default">
        <div class="panel-heading"><h4>Invite friend</h4></div>
        <div class="panel-body">                        
        <form action="">              
            <div class="col-md-4 col-lg-4">
              <label class="control-label" for="friend">Enter email address</label>              
              <input type="email" class="form-control" name="friendemail" id="friendemail" placeholder="sam@uncle.com" required><br>                                
              <?php 
              echo '<input type="hidden" name="invitename" value="'.$_SESSION["user_name"].'">' ;                 
              echo '<input type="hidden" name="invite-url" value="'.$_SERVER['REQUEST_URI'].'">';
              echo '<input type="hidden" class="invite-product" name="invite-product-name">';
              ?>
              <input type="button" id="btn-submit" name="submit" value="Invite" class="btn btn-primary" />
            </div>            
        </form>
        <div class="mail-message"></div>
        </div>
        </div>
  <?php 
  }else{
  }?>

And change AJAX function as: 并将AJAX功能更改为:

$(function () {
    $('#btn-submit').on('click', function (e) {
      e.preventDefault();
      $.ajax({
          type: 'get',
          url: 'invite-friend.php',
          data: $('form').serialize(),
          success: function () {
              $(".mail-message").html('<div class="alert alert-success"><strong>Success!</strong> Indicates a successful or positive action.</div>');
          }
       });
    });
});

We do not need a submit button in form if we using AJAX. 如果我们使用AJAX,我们不需要表单中的提交按钮。

Dude you are simply making your code complex ... you are doing form submit + ajax submit .I think there you are going wrong. 老兄,你只是让你的代码变得复杂......你正在做表单提交+ ajax提交。我认为你出错了。 Just try these: Remove action="friend-invite.php" from your form tag and try . 试试这些:从表单标签中删除action =“friend-invite.php”并尝试。 if this does not help than make your button input type button or use button tag. 如果这比按钮输入类型按钮或使用按钮标签没有帮助。 just try all these it should work. 试试这一切应该有效。 If all this does not work than give id and name to your form ,,and use to submit as: 如果所有这些都不起作用,而不是为您的表单提供ID和名称,并使用提交为:

$(function() {
            $("#form_id").on("submit", function(event) {

Try all these 尝试所有这些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM