dousha1873
2013-11-23 21:18
浏览 52
已采纳

如何从PHP / AJAX / jQuery中的表单外的JavaScript变量中保存数据?

PHP newbie here. I want to save a variable (from jQuery) in PHP/MySQL, but this variable is outside the form. The values from the form elements inside the form is being saved fine though. The variable name in this case is 'mode', and I want the 'mode' to be sent to PHP.

Here's code :

HTML form;

<form action="submit.php" method="post" id="myform">
 <textarea id="source1" name="source1"></textarea>
<textarea id="source2" name="source2"></textarea>
 <input type="image" src="images/save.png" name="submit" id="submit" title="Save"  class="save-button"/>    
</form>

jQuery / AJAX:

mode = 1;  // This value needs to be stored/saved

// AJAX form save
$("form#myform").submit( function () {    
$.post(
'submit.php',
$(this).serialize(),
function(data){
....
}
);

PHP:

$submit_time = date('Y/m/d H:i:s');
$content1 = $_POST['source1'];
$content2 = $_POST['source2'];
$mode = $_POST['mode'];  // This value needs to be stored/saved

Is the solution to create a hidden form field inside the form?

图片转代码服务由CSDN问答提供 功能建议

PHP新手在这里。 我想在PHP / MySQL中保存一个变量(来自jQuery),但是这个变量在表单之外。 表单中的表单元素的值虽然保存得很好。 在这种情况下,变量名称是'mode',我希望'mode'被发送到PHP。

这是代码:

HTML form;

 &lt; form action =“submit.php”method =“post”id =“myform”&gt; 
&lt; textarea id =“source1”name =  “source1”&gt;&lt; / textarea&gt; 
&lt; textarea id =“source2”name =“source2”&gt;&lt; / textarea&gt; 
&lt; input type =“image”src =“images / save.png”  name =“submit”id =“submit”title =“Save”class =“save-button”/&gt;  
&lt; / form&gt; 
   
 
 

jQuery / AJAX:

  mode = 1;  //需要存储/保存此值
 
 // AJAX格式save 
 $(“form#myform”)。submit(function(){
 $ .post(
'submit.php',  \ N $(本).serialize(),
function(数据){
 .... \ N} 
)的; 
   
 
 

PHP:< / p>

  $ submit_time = date('Y / m / d H:i:s'); 
 $ content1 = $ _POST ['source1']; 
 $ content2 =  $ _POST ['source2']; 
 $ mode = $ _POST ['mode']; //需要存储/保存此值
   
 
 

是 在表单中创建隐藏表单字段的解决方案?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongwu3596 2013-11-23 21:22
    已采纳

    If the variable is not inside the form it doesn't get send to the server.

    The solution is to put the variable into hidden field inside the form

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doujing1858 2013-11-23 21:23

    Try:

    // AJAX form save
    $("form#myform").submit( function () {    
        $.post('submit.php', $.extend($(this).serializeArray(), { mode: mode }), function(data){
            ....
        });
    });
    
    评论
    解决 无用
    打赏 举报
  • doushi1473 2013-11-23 21:25

    jQuery's serialize() serializes the form into a querystring, so you really just have to add to that string :

    mode = 1;
    
    $("form#myform").submit( function (e) {    
        e.preventDefault();
        var data = $(this).serialize() + '&mode=' + mode;
        $.post('submit.php', data, function(data){
    
        });
    });
    

    or to submit the form with a hidden input:

    $("form#myform").submit( function (e) {  
        e.preventDefault();
        $(this).append( $('<input />', {type:'hidden', name:'mode', value:mode}) );
        this.submit();
    });
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题