开发者

Submit a URL as data in cakePHP

I am using cakePHP 1.26.

I got an Input Text box which contains a URL and I want to submit the URL and stored it in the Database using Jquery AJAX.

Here is the HTML part:

<input type="text" id="testing" value="https://stackoverflow.com/questions/ask">

This is the JQuery part:

  var whatContent=$("#testing").val();
      var curl="http://localhost:8080/test/grab/"+whatContent;
      $.ajax({
      type: "POST",
      url: curl,   
      success: function(data) {    
      alert(data);}
      });

This is the code for the Action in the Controller:

function grab($w=null){
   if($w!=null){
     return $w;
    }
}

The code worked and I could see the Alert Message pop up, but there was something missing in the message. I mean I supposed to see the whole URL like this:

https://stackoverflow.com/questions/ask

But not, I just saw part of it instead:

http://stackoverflow.com

Later I altered the value in the Input Text box like this:

<input type="text" id="testing" va开发者_如何学运维lue="https://stackoverflow.com/faq">

But again, the returned value was still

http://stackoverflow.com

cakePHP seemed to conside the URL as some parameters rather than a URL.

Please help


When you append the content to the end of your "curl" variable like you are, you are attempting to add it to be retrieved through a GET variable and will get a result in a request like http://localhost:8080/test/grab/http://stackoverflow.com/questions/ask. Clearly this is an invalid request. Your GET variable parsing is not going to be consistent and a dangerous way of passing data back to your controller (especially if users will be able to edit the appended value).

Instead, you should use the data attribute in jQuery to pass this information back in your POST request as described in the instructions here: http://api.jquery.com/jQuery.ajax/

On the Cake side, you'll be able to receive this value as $this->data['IDValueYouConfigured']. For example, if your AJAX request was like:

  var whatContent=$("#testing").val();
  var curl="http://localhost:8080/test/grab/";
  $.ajax({
  type: "POST",
  url: curl,
  data: "formValue="+whatContent,   
  success: function(data) {    
  alert(data);}
  });

where formValue is the IDValueYouConfigured that I mentioned earlier.

More importantly, you seem to be misunderstanding proper use of the Cake framework and could be performing all of these functions MUCH more simply using things like the JsHelper, FormHelper, etc. I would recommend using the most RECENT version of Cake (1.3.3) and follow through the Blog tutorial at least once. This will lead to better questions which will be more likely to get helpful answers. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜