Welcome Guest, Not a member yet? Register   Sign In
passing data from javascript
#1

(This post was last modified: 07-16-2015, 09:22 AM by cupboy.)

Is there a way to pass data from javascript (jQuery) that will handle certain characters like slashes?
Code:
$.post("/index.php/c1/insertOffer/" + data_ID + "/" + data_offertext + "/" + data_accepted);        

The above works fine unless someone puts a slash in the data, but there's surely other characters that would mess it up too. I guess I could use a cookie but would prefer not to.
Reply
#2

http://api.jquery.com/jquery.post/

Code:
$.post(url, data);

Don't pass anything through the URI which might include special characters (like slashes), just pass it through the data parameter in your JavaScript and retrieve it from $this->input->post() in your PHP code.

Code:
// JavaScript
$.post('index.php/c1/insertOffer', { "data_ID": data_ID, "data_offertext": data_offertext, "data_accepted": data_accepted });

// PHP
public function insertOffer()
{
    $dataId = $this->input->post('data_ID');
    $dataOffertext = $this->input->post('data_offertext');
    $dataAccepted = $this->input->post('data_accepted');
    // ...
}
Reply
#3

(This post was last modified: 07-16-2015, 01:17 PM by cupboy.)

Although that code looks good compared to other code I have found on the web as soon as I add the data part it never even makes it to the controller.
Reply
#4

I missed a slash at the beginning of the URL, if you happened to copy my code directly over.

You might also want to try ditching the post() shortcut method and just using the ajax() method directly:

Code:
$.ajax({
    url: '/index.php/c1/insertOffer/',
    type: 'POST',
    data: {
        "data_ID": data_ID,
        "data_offertext": data_offertext,
        "data_accepted": data_accepted
    }
});

Also, your controller's insertOffer() method has to either require no parameters or have defaults for all of the parameters. The data has to be retrieved from the $_POST array or $this->input->post(), as it's no longer being passed in the URI.
Reply
#5

Hi,
you should better use "POST" as request type instead of "GET", if your project require you to use "GET" request type, so escape string from you js script first. I don't know exactly which scrpit to give as example, but you can look for encoding URI http://www.w3schools.com/jsref/jsref_enc...ponent.asp
NexoPOS 2.6.2 available on CodeCanyon.
Reply
#6

(07-17-2015, 06:54 AM)mwhitney Wrote: I missed a slash at the beginning of the URL, if you happened to copy my code directly over.

You might also want to try ditching the post() shortcut method and just using the ajax() method directly:


Code:
$.ajax({
   url: '/index.php/c1/insertOffer/',
   type: 'POST',
   data: {
       "data_ID": data_ID,
       "data_offertext": data_offertext,
       "data_accepted": data_accepted
   }
});

Also, your controller's insertOffer() method has to either require no parameters or have defaults for all of the parameters. The data has to be retrieved from the $_POST array or $this->input->post(), as it's no longer being passed in the URI.

works fine after putting that slash in... funny thing is my original code had the slash but when adding the data part I did it without the slash and never noticed it
Reply




Theme © iAndrew 2016 - Forum software by © MyBB