我应该在以下代码中添加些什么,以便它可以将按钮值onclick保存在php变量中

实际上,我制作了一个表单并在表单中有许多按钮,因此我想知道如何完成在提交表单时单击按钮是否会存储该按钮的值,并在单击到最终提交按钮后将发送邮件的方法。发送所有输入字段值,包括我单击的按钮值。


<script type="text/javascript">

$(document).ready(function() {

  $('#frmemail').submit(function(event) {

    $.ajax({

      type: 'POST',

      url: '',

      data: $('#frmemail').serialize(),

      success: function() {

        $('.success').fadeIn(1000)

      }

    });

  });

});

</script>

     <?php

   if (isset($_POST['garden']))

   {

     $but= $_POST['garden'];

   }

 ?>

 <?php

$name = $_POST['form_name'];

$email = $_POST['form_email'];

$message = $_POST['form_msg'];

$garden = $_POST['garden'];

$to = "admin@rsfcrm.com";

$subject = "RIA Emails";

$body = "Name: ".$name."\nEmail: ".$email."\nGarden".$but."\nMessage:".$message;

$headers = "From: ". $email;

    if (mail($to,$subject,$body,$headers))

{echo "<script>window.alert('Successfull!');

    </script>";  

}else

{echo "<script>window.alert('Fialed');

    </script>"; 

}?>    

<div class="container">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section>

  <form enctype="multipart/form-data" id="frmemail" method="post" action="test2">

    <fieldset class="margin-b">

      <legend>Contact Me</legend>

      <label for="form_name">Name:<input name="form_name" type="text" value="" required autofocus ></label>

      <label for="form_email">Email:<input name="form_email" type="email" value=""></label>

      <label for="form_msg">Message:<textarea name="form_msg" rows="5"></textarea></label>

    </fieldset>

    <button type="submit" id="garage" name="garden" value="gardening">button</button>

    <input type="submit" name="submit" id="submit" value="Submit">

  </form>

  </section>

</div>  </body>`


MMMHUHU
浏览 119回答 3
3回答

qq_笑_17

<input type='hidden' name='garage_val' id='garage_val' value='' /><script>$("#garage").click(function(){&nbsp; &nbsp; $("#garage_val").val($(this).val());});</script>

qq_花开花谢_0

您可以使用jquery和隐藏变量功能。例如,在第一个按钮上单击“将值存储在隐藏字段中”,在第二个按钮上单击“提交表单”。如果您想要代码,那么我可以粘贴在这里

青春有我

在您form添加此输入字段中<input type='hidden' name='gardening' value='gardening' />在您的PHP代码中<?php$gardening= $_POST['gardening'];echo $gardening;?>jQuery的&nbsp;$('#frmemail').submit(function(event) {&nbsp; &nbsp; &nbsp;$.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'yourphpfilename.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: $('#frmemail').serialize(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; });不需要jquery click函数来执行此操作,此值将与表单一起传递,希望这会有所帮助(或者)如果您希望仅在按钮上传递值,请使用此选项Html<input type='hidden' name='gardening'&nbsp; class="hidden"/>jQuery的$("#garage").click(function(e){&nbsp;&nbsp; e.preventDefault();&nbsp; $('.hidden').val('gardening');});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript