更新$ _POST并通过jquery传递给php而无需提交 [英] updating $_POST and passing to php via jquery without submit

查看:108
本文介绍了更新$ _POST并通过jquery传递给php而无需提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:这可能会非常简单,但由于其他人的#%^ $!&代码,每次提交都会清除页面上所有表单上的所有数据,所以我不能使用.submit()



这是我正在做的事情:



步骤1:通过Javascript打开页面上的链接,打开一个弹出窗口。该弹出窗口中有一些字段可以填写,然后使用window.opener.getElementById('hiddenX').value,然后更新原始页面上各种隐藏的HTML元素,并关闭弹出窗口。



第2步:卸载弹出窗口时,html元素通过Javascript进行修改。



第3步:添加更新的html元素返回到$ _POST页面

第4步:一些php代码使用$ _POST中的值以及之前的php代码生成的值运行php函数创建和发送电子邮件。

我不认为我可以通过GET风格的URL传递值,因为一些值是非常长的文本字符串,并且tehre也可能是一些数组。我已经尝试使用.post(window.location,{val1:1stvalue})与包含来自步骤4的php代码的div上的.load()组合。我认为div更新,但POST值仍为空。任何想法?

脚本:

  $(。email1 ).on('click',function(event){
var win1 = window.open(../ sub1.php,_ blank,height = 450,width = 510,status = yes, ($'$');
$(win1).on('unload',function(event){
if(document.getElementById('hidden1')。value('toolbar = no,menubar = no,location = no');
$ tosend2 = document.getElementById('hidden3')。value;
...... //对$ tosend2和页面上其他元素的各种更改
$ .post(window.location,{hidden3:abc}); //临时使用abc而不是$ tosend2
$(#emaildiv).load(location.href +#emaildiv2> ; *,);

}
});
});

html / php:

 < div style =display:hiddenname =emaildivid =emaildiv> 
< input type =hiddenname =hidden1id =hidden1value =z/>
< input type =hiddenname =hidden3id =hidden3value =z/>
< / div>
< div style =display:hiddenname =emaildiv2id =emaildiv2>
<?php
echo< div class ='error'>什么是val ?:。 $ _POST ['hidden1']。 < / code>< / div>;
if($ _BOST ['hidden1']!=z){
echo'< div class = '错误'>现在是什么?:。 $ _POST ['hidden1']。 < / code>< / div>;
// emailstuff($ locnlist,$ _POST ['hidden1'],$ _POST ['hidden3']);
// echo发送邮件;
}
}
?>
< / div>


解决方案

我通过移动电子邮件功能和php div到一个单独的php页面,名为sub2.php,然后通过ajax将post值发送到该页面

  $ .ajax( {
type:'post',
url:'/routing/sub2.php',
data:{hidden1:document.getElementById('hidden1')。value,
hidden2:document.getElementById('hidden2')。value,
hidden4:document.getElementById('hidden4')。value,
locnlist:loclist},
success:function(){
alert(Email has been sent!);
}
});

这实际上并不能解决最初的问题,但它也可以。
如果有人有更好的解决方案,我会很高兴知道它是什么

caveat: this would probably be pretty simple with a submit, but because of someone else's #%^$!& code, every submit clears all data on all forms on the page, so i cannot use .submit()

here's what i'm trying to do:

step 1: a link on a page, via Javascript, opens a popup. that popup has a some fields you fill it out, and using window.opener.getElementById('hiddenX').value, various hidden html elements on the original page are then updated, and the popup is closed.

step 2: on unload of the popup, the html elements are modified via Javascript.

step 3: the updated html elements are added back to $_POST on the page

Step 4: a bit of php code runs a php function using the values from $_POST as well as the values produced by earlier php code to create and send an email.

I do not think i can pass the values via a GET-style url since some of the values are very very long text strings, and tehre will probably be some arrays as well. I have tried using .post(window.location, {val1 : "1stvalue"}) in combination with a .load() on the div containing the php code from step 4. i think the div updates, but the POST value remains empty. any ideas?

script:

$(".email1").on('click', function (event) {
        var win1 = window.open("../sub1.php","_blank","height=450,width=510, status=yes,toolbar=no,menubar=no,location=no");
         $(win1).on('unload', function (event){
             if(document.getElementById('hidden1').value != "z"){
              $tosend2 = document.getElementById('hidden3').value;
              ...... // various changes to $tosend2 and other elements on the page
              $.post( window.location, { hidden3 : "abc"  }); //temporarily use "abc" instead of $tosend2
              $( "#emaildiv" ).load(location.href+" #emaildiv2>*","" );

              }
         });
});

html / php :

<div style="display : hidden" name="emaildiv" id="emaildiv">
   <input type="hidden" name="hidden1" id="hidden1" value="z" />
   <input type="hidden" name="hidden3" id="hidden3" value="z" />
</div>
<div style="display : hidden" name="emaildiv2" id="emaildiv2">
<?php 
   echo "<div class='error'> what is the val?: " . $_POST['hidden1'] . " </code></div>";
  if( !empty($_POST['hidden1']) ){
    if( $_POST['hidden1'] != "z" ){
        echo "<div class='error'> what is it now?: " . $_POST['hidden1'] . " </code></div>";
    //emailstuff($locnlist, $_POST['hidden1'], $_POST['hidden3']);  
    //echo "email sent";
    }   
}
?>
</div>

解决方案

I solved this by moving the email function and php div to a separate php page, called sub2.php, and then sending the post values to that page via ajax

                        $.ajax({
                            type: 'post',
                            url: '/routing/sub2.php' ,
                            data: { hidden1 : document.getElementById('hidden1').value , 
                                    hidden2 : document.getElementById('hidden2').value , 
                                    hidden4 : document.getElementById('hidden4').value , 
                                    locnlist : loclist },
                            success: function () {
                            alert("Email has been sent!");
                            }
                        });

this doesn't actually solve the initial issue, but it works just as well. if someone has a better solution, i'd be very pleased to know what it is

这篇关于更新$ _POST并通过jquery传递给php而无需提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆