post并进入相同的ajax请求

问题描述 投票:0回答:3

在上述AJAX帖子中,我将使用用户ID

保存表单
我可以获取我在Ajax请求中保存的表单的表单ID。如果是这样?

我已经尝试了Ajax分开尝试。
我可以吗
编辑:

我可以返回Ajax Post方法的任何值。由于我想返回保存的表单ID。

Edit:

alert("Data Saved: "+msg); gives as Data Saved: {"forms":[{"id":"41"},{"id":"35"},{"id":"34"},{"id":"33"},{"id":"32"},{"id":"22"},{"id":"3"},{"id":"2"},{"id":"1"}]}

上面是返回的值,我只想要id 41我应该如何获得它?

Edit:

$.ajax({ type: "POST", url: "http://localhost/FormBuilder/index.php/forms/saveForm/"+user_id, datatype: 'json', data: "formname="+formname+"&status="+status, success: function(json){ alert( "id is : " + json.forms[0].id); }//success });//ajax

我使用上述代码尝试了一下,但是我无法收到警报消息。

我的控制器代码喜欢

function saveForm() { //$userId=$this->Session->read('userId'); $this->data['Form']['name']=$this->params['form']['formname']; $this->data['Form']['created_by']=$this->Session->read('userId'); $this->data['Form']['status']=$this->params['form']['status']; $this->data['Form']['access']="Private"; $userId=$this->Form->saveForms($this->data); $formid = $this->Form->find('all', array('fields' => array('Form.id'), 'order' => 'Form.id DESC' )); $this->set('formid',$formid); }

和我的save_form.ctphos

      <?php
     $data=array();

      ?>
     <?php foreach ($formid as $r): 


      array_push($data, array('id' => $r['Form']['id']));

    endforeach; 

     echo json_encode(array("forms" => $data));

    ?>

是的,您可以做到这一点。您可以使用或不带查询字符串的任何URL发布到任何URL。

您可以访问$ _GET数组中的任何常规查询字符串参数,或者在您的情况下,将其分解为

$_SERVER['REQUEST_URI']
。发布的数据将按预期为$ _ post。

已编号的Q.#1“我可以返回Ajax Post方法的任何值吗?”

是的,您可以将您想要的任何内容作为Ajax响应返回。您对此响应的处理取决于您的JavaScript。

pedited Q.#2“我如何读取响应中的值”
jquery ajax cakephp
3个回答
7
投票

$.ajax({ type: "POST", url: "http://localhost/FormBuilder/index.php/forms/saveForm/"+user_id, datatype: 'json', data: "formname="+formname+"&status="+status, success: function(json){ alert( "id is : " + json.forms[0].id); }//success });//ajax

the the the the the the the the Buretly there Oss Onky似乎有效...

表单页:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript" src="../../scripts/ajax.js"></script> <script type="text/javascript"> function send_ajax(){ get = '?get_text=' + document.getElementById("get_text").value; get = get +'&get_text2=' + document.getElementById("get_text2").value; post = "post_text="+document.getElementById("post_text").value; post = post + "&post_text2="+document.getElementById("post_text2").value; path = 'prosses.php'; // path to server side prossessing page. element = 'result'; // placeholder for the html returned from the server side prossessing page. ajax(path,get,post,element); } </script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>ajax testing page</title> </head> <body> GET1: <input type="text" id="get_text" onkeyup="send_ajax()" /><br /> GET2:<input type="text" id="get_text2" onkeyup="send_ajax()" /> <br /><br /> <form action="" method="post"> POST1:<input id="post_text" type="text" /><br /> POST2:<input id="post_text2" type="text" /><br /> <input type="button" value="Button" onclick="send_ajax()" /><br /> </form> <span id="result">Result will appear here</span> </body> </html>

ajax脚本:

var xmlhttp; function ajax(path,get,post,element){ ajax_suite(path,get,post); function ajax_suite(path,get,post){ xmlhttp=GetXmlHttpObject(); if (xmlhttp==null){ alert ("http requests are not supported by your browser"); return; } if(get == ""){ rand = "?sid="+Math.random(); }else{ rand = "&sid="+Math.random(); } var url=path + get + rand; xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", post.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(post); } function stateChanged(){ if (xmlhttp.readyState==3){ document.getElementById(element).innerHTML="Loading..."; } if (xmlhttp.readyState==4){ document.getElementById(element).innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject(){ if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject){ // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } }

PHP处理页面:

GET1 = <?php echo $_GET['get_text']; ?><br> GET2 = <?php echo $_GET['get_text2']; ?><br> POST1 = <?php echo $_POST['post_text']; ?><br> POST2 = <?php echo $_POST['post_text2']; ?><br>

我希望这有帮助 Blaise.



1
投票
var myJavaScriptVariable = 'world'; $.ajax({ type: "POST", url: "http://localhost/FormBuilder/index.php/forms/saveForm/"+user_id+?myFirstGetParamater=hello&mySecondGetParameter='+ myJavaScriptVariable+'", data: "formname="+formname+"&status="+status, success: function(msg){ // alert( "Data Saved: " + msg); }//success });

我假设由于Ajax请求,该表格为 由PHP脚本保存,您想获得保存的表单的ID。

如果是这样,将您的php脚本设置为the保存的表单的ID, 仅输出。然后您可以使用:
echo

cop请求将把一些数据返回给客户端。如果您的服务器端脚本返回任何数据,它将传递到
success: function(id{ // alert( "Data Saved: " + id); }//id is the id });//ajax

函数中。 从那里解析!


您无法混合帖子并输入相同的AJAX请求,因为AJAX请求是HTTP请求,并且单个HTTP请求只有一种方法和仅有一种方法(获取,头部,张贴,张贴,put,put,delete,trace或connect,虽然我从未见过后两个使用过,放在和删除并不常见)。

从那以后,尚不清楚您所说的“形式ID”是什么意思 - 您要发布的URL是在帖子成功时返回某些唯一标识符的URL吗?


1
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.