POST and Get in the same Ajax request

I am using ajax post in my application, for example

 $.ajax({
     type: "POST",
    url: "http://localhost/FormBuilder/index.php/forms/saveForm/"+user_id,

    data: "formname="+formname+"&status="+status,
     success: function(msg){
     // alert( "Data Saved: " + msg);
                }//success
 });//ajax

In the ajax post above, I save the form with user id

Can I get the form form identifier that I saved in the Ajax request. If so, how?

I tried with Ajax to get separately. But here I want to mix and message and get .. Can I do this. EDIT:

COuld I returns any value for the Ajax POST method. Since I want to return the form identifier of the saved form.

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"}]}

The above is that the return value I only want is id 41, how should I get it?

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

Even I tried it with the code above, as suggested, but I can not get the warning message.

My controller code is similar to

     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);

    }

And my save_form.ctp file has

      <?php
     $data=array();

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


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

    endforeach; 

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

    ?>
+3
6

, . POST URL-, .

$_GET $_SERVER['REQUEST_URI']. POSTed $_POST, .

Q. # 1 " - AJAX POST?"

, , , Ajax. , Javascript.

Q. # 2 " "

JSON, jQuery, ,, . , - :

$.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
+7

, .

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
});
+1

, ajax PHP script, .

, PHP echo , , . :

 success: function(id{
         // alert( "Data Saved: " + id);
                                }//id is the id
 });//ajax
0

. script , success: function(data){}. !

0

, , , , ...

:

<!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 script:

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>

,

.

0

POST GET AJAX, AJAX HTTP-, HTTP- (GET, HEAD, POST, PUT, DELETE, TRACE CONNECT, , , PUT DELETE - ).

, , " " - URL-, POSTing, - POST?

-1

Source: https://habr.com/ru/post/1714851/


All Articles