any idea how I can get a string to return from an ajax call taking the href from an object using: (javascript)

$('herpy').attr('href').toString() 

into a PHP controller as a string so it can be used as a parameter in sql code?

Everything I try results in this bizzarre array being created once the data is passed. No matter what !

Cheers!

Dani AI

Generated

Good to see got it working. Below is a short, practical checklist and examples that explain the usual pitfalls when sending an anchor href from the browser to a CakePHP controller and then using it in a query.

A reliable client-side pattern is to send the href as a named field in an AJAX request so the browser takes care of encoding. Use data: { href: href } (or encodeURIComponent(href) if you build your own query string). See the jQuery AJAX docs for options and encoding behavior: jQuery.ajax. If you send JSON in the body, set the content type and stringify the object explicitly.

On the CakePHP side the value will appear in the request object depending on how you sent it. For Cake 2.x check $this->request->data (POST) or $this->request->query (GET); for Cake 3.x use $this->request->getData() / $this->request->getQuery(). If you post raw JSON in Cake 2.x you can decode the input with $this->request->input(). The official request/response docs are here: CakePHP 2.x request/response and CakePHP 3.x request/response.

Never concatenate user data into SQL. Use Cake model find/save methods or parameter binding. Examples:

# Cake 2.x
$href = $this->request->data['href'];
$rows = $this->MyModel->find('all', ['conditions' => ['MyModel.field' => $href]]);
# Cake 3.x
$href = $this->request->getData('href');
$results = $this->MyModel->find()->where(['field' => $href])->all();

If things still look like a strange array: inspect the Network tab to see the raw request payload, confirm contentType and dataType settings, and log the incoming $this->request->data (or getData) on the server. Also check for CSRF or Security component rules that can block AJAX POSTs. For general safe DB usage and prepared statements see the PHP PDO docs: PDO prepared statements.

Ok guys after 20 minutes more of tinkering arround I was able to figure it out. Doing what I was orignally doing... O_O

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.