简体   繁体   English

在php,ajax和javascript中使用post方法

[英]Using post method with php,ajax and javascript

I have struggle passing the value of an input from a form on a page to an other one, so here is the code : (PS: I'm french so sorry If my grammar is bad and I hope it's understandable) This my page with the form : 我很难将输入值从页面上的表单传递给另一个表单,所以代码如下:(PS:我是法语,对不起,如果我的语法不好,我希望它是可以理解的)表格 :

Edit : I had made two mistake in this form, that have been corrected now. 编辑:我在此表格中犯了两个错误,现在已纠正。 First I forgot the ' ' around mission_trans_select : onclick="document.getElementById('mission_trans_select').value=(this.value) and second, an echo was missing in value="<?php echo $line['MIS_ID_MISSION']; 首先,我在mission_trans_select周围忘记了'': onclick="document.getElementById('mission_trans_select').value=(this.value) ,其次,在value="<?php echo $line['MIS_ID_MISSION'];缺少echovalue="<?php echo $line['MIS_ID_MISSION'];

<html>
<form name="fListTran" id="fListTran" method="POST" action="index.php?page=missiontransfert">
    <div class="container">    
        <div id="loginbox"  class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">     
            <div class="text-center">
                <div class="panel panel-primary" >
                    <div class="panel-heading">
                        <div class="panel-title"><strong>Liste des missions de transfert</strong></div>
                        <div id="float"><a href="#"></a></div>
                    </div> 
                    <div class="panel-body" >
                        <input name='name' id='mission_trans_select' type='hidden' value='' />
                            <strong>
                            <?php
                                echo "Il y a : ".$_Nbr_Missions_." missions trouvée(s).";
                                For($i=1;$i<=$_Nbr_Missions_;$i++){ //peut être mettre un max et donc tester le nombre de resultat
                            ?>
                            </strong>
                            <div class="btn-group-justified" id="mis-tran">
                            <a href="index.php?page=missiontransfert">
                                <button class="btn btn-default btn-lg btn-block" name="mission_tran" id="<?php echo $i ;?>" type="submit" value="<?php echo $line['MIS_ID_MISSION']; ?>" onclick="document.getElementById('mission_trans_select').value=(this.value);">
                                <?php 
                                    echo $line['MIS_ID_MISSION'] .' - [' . $line['MIS_ID_DO'] . '] Emplacement actuel : ' 
                                            . $line['MIS_ID_AREA_SRC'] .'-'.$line['MIS_ID_BAY_SRC'].'-'.$line['MIS_ID_RACK_SRC'].'-'
                                            . $line['MIS_ID_STAGE_SRC'] .'<br> Priorité : '.$line['MIS_PRIORITE']
                                            .'- Produit : '.$line['PRO_LITERAL'] ; 
                                ?>
                                </button> 
                            </a>
                        </div>  
                        <?php
                            $line = oci_fetch_array($reqMisTranCount, OCI_ASSOC);
                            }
                        ?>
                        <a id="retour" class="btn btn-lg btn-warning btn-block" href="index.php?page=menu">
                            <span  class="fa fa-arrow-circle-left"></span> Retour</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

So to explain it I display a list of mission represented by buttons that the user can click on and each button is link to the page that I want to display an other form that require the value associated to the pressed button. 因此,为了解释它,我显示了一个由用户可以单击的按钮代表的任务列表,每个按钮都链接到该页面,我想要显示其他形式的页面,该形式需要与按下的按钮相关联的值。

So as the id of the buttons depends of the numbers of row I obtain from my select query that I didn't show here but that works fine, I can't get the value from it so onClick of the button I set the value of the hidden input to the value of it. 因此,由于按钮的ID取决于我从选择查询中获得的行数(未在此处显示,但可以正常工作),因此无法从中获取值,因此onClick按钮的值设置为隐藏值的输入。

The form as for action : index.php?page=missiontransfert , so in the index page I called my script loadJS.php where I try to use the post method with ajax but I don't get it. 动作形式为: index.php?page=missiontransfert ,因此在索引页面中,我调用了脚本loadJS.php,在该脚本中尝试将post方法与ajax结合使用,但我不明白。 Here is what I do so far : 这是我到目前为止所做的:

<script>
if (document.getElementById("mission_trans_select").value!=''){
    var mis_id = document.getElementById("mission_trans_select").value;

}

function get_mis_id(element){                    
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("POST","./ajax/ajaxlistetransfert.php",true);
    xmlhttp.setRequestHeader("Content-Type", "./transfert_list.php");
    xmlhttp.send(mis_id);
}
</script>

I don't know if it's correct or if it's the good way to do it... And also I don't know what to put into my ajax file (ajaxlistetransfert) 我不知道这是正确的还是执行此操作的好方法...而且我也不知道要在我的ajax文件中放入什么内容(ajaxlistetransfert)

And finally is this the good way to obtain the post result in my file missiontransfert.php : 最后,这是在我的文件missiontransfert.php中获取发布结果的好方法:

$_SESSION['MISMISSION']=$_POST['name'];

The 'name' was the name from the hidden input in my first form. “名称”是我的第一种形式中来自隐藏输入的名称。 Thanks for your help and time ! 感谢您的帮助和时间!

As you've included jQuery in your question tags, you can use simplified POST request syntax: 在问题标签中包含jQuery后,就可以使用简化的POST请求语法:

$.post('/ajax/ajaxlistetransfert.php',{
    mission_id: your_variable_holding_the_id
},function(result){
    alert(result); // alerts output of requested file.
})

and have your ajax PHP file make necessary operations with $_POST['mission_id'] on the database/files and echoing the output 并让您的ajax PHP文件对数据库/文件进行$_POST['mission_id']必要的操作并回显输出

(ie. 'success'/'failure' string or a form element code that then could be appended to the requesting page) (即“成功” /“失败”字符串或可以添加到请求页面的表单元素代码)

which then will be accesible to javascript as result 然后将入店到JavaScript作为result

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

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