Doing A GET request in CodeIgniter using URI Segments - codeigniter

If I have a form like this:
<form method="GET">
<input type="text" name="search" />
</form>
and if I Submit it, it generates a URL like this: http://exmaple.com/search/results?search=text1+text2
Now this doesn't work with my code. How do I make it use URI Segments, so the generated url looks like this: http://exmaple.com/search/results/search/text1+text2
More Clarification
In other words, What I want is that when I do a get request using a simple html form, the url it generates look like the codeigniter's URL i.e example.com/class/function/param1/param2
and not example.com/class/function?param1&param2

Using codeigniter routing you can achieve http://exmaple.com/search/results/search/text1+text2
by using putting this in your route
$route['search/results/search/(:any)'] = "search/results?search=$1"

in your codeigniter config file $config['enable_query_strings'] is set to false, so the url looks like http://example.com/search/results/search/text1+text2
if you want url like http://example.com/search/resutls?search=text1+text2 then set $config['enable_query_strings'] to true;

Related

CS CART call a controller at add on front end with microformat ajax

I am very new to cs cart. I have read their documentation about calling Ajax with form micrformats. I am able to create a Ajax request. But I would like to call a controller inside my add-on controllers/frondend/ directory names as mycheckout.php. I am using the a hidden file dispatch with the valu mycheckout.mymode. Can any one help me. I actually want to perform some action in my controller. But their documentation did not help me. Thanks in advance.
You can do something like this (use extra "cm-ajax-force" or "cm-ajax-full-render" classes if needed):
<form class="cm-ajax" name="your_name" action="{""|fn_url}" method="post">
<input type="hidden" name="result_ids" value="div_id_to_be_updated" />
{include file="buttons/go.tpl" but_name="mycheckout.mymode" alt=__("Ajax button")}
</form>
Or you can use any button you want (even <input type="submit" name="mychechkout.mymode" value="Ajax!">)
Note that the Ajax request can be also done via simple link:
<a class="cm-ajax cm-post" href="{"mycheckout.mymode?param1=value1&param2=value2"|fn_url}" data-ca-target-id="div_id_to_be_updated">Ajax!</a>

Issue url scheme of code-igniter

I have a form like
<form action="abc/1" method="post">
</form>
I want every time this form is submitted my URL remains same like suppose my current URL was
http://localhost/abc/1 after form submit it should be again
http://localhost/abc/1
but instead this it become
http://localhost/abc/1
http://localhost/abc/abc/1
http://localhost/abc/abc/abc/1 each time I press submit button in form.
its something related to URL schemes of mvc in code-igniter
The action of your form is relative to your current position.
If you are on http://example.com/contact and your form's action is set to contact/send the form well send to http://example.com/contact/contact/send Now, this would be easy fixed by either removing the contact/ part form the action attribute or by adding a / in the beginning of your action attribute, so the path is absolute - /contact/send.
Doing this in CodeIgniter should be relatively easy, as you can use the URL Helper to point to the correct URL's in your application.
<form action="<?php echo site_url('abc/1'); ?>" method="post">
</form>
This example will always point you to the page relative to your base_url and index_page settings in application/config/config.php.
In your case, mentioned in the comments, something along with <?php echo site_url('home/authenticateUser/' . $user_id); ?> would probably be the answer.
Try like
<form action="/abc/1" method="post">
</form>
or else you better to use site_url like
<form action="<?php echo site_url('abc/1');?>" method="post">
</form>
Changing the action from "abc/1" to "/abc/1" will work, but if you are building your app in a subdirectory, as is usually the case on a localhost development environment, this will revert to your htdocs folder, as opposed to your required folder.
I would recommend one of two options:
<form action="<?php echo base_url(abc/1); ?" method="post">
<?php form_open('abc/1')' ?>
Number 2 is the better one, as it works with CodeIgniter's CSRF functionality, and adds the base_url() automatically.
Hope this helps.

codeigniter 2.1.0 Routing difficulties

I can not load controller with
http://localhost/index.php?controller/method
But if i does not use '?' and try to load a controller from view then duplicate URL produce.
As an example of above, if i add this link in form action in view then result url will be
http://localhost/index.php/controller/index.php/controller/method
How to solve it? I did not use htaccess file
Use the site_url() function or, as #JohnFable stated, use the form_open function.
<form method="post" action="<?= form_open('controller/method'); ?>">
or
Controller/Method
or
<?= form_open('controller/method'); ?>
This will ensure that your "base url", i.e. http://localhost is prepended correctly to the URL you want to view, i.e. http://localhost/index.php/controller/method or if you have set it up to, http://localhost/controller/method
Gavin

Requests with AJAX in a portlet (Liferay)

I have an issue with my portlet and I don't know exactly how to solve it.
My portlet adds or retrieves info from liferay's DB by inserting a name in 2 text fields.
After pressing the submit button, I see the response from the server, a JSON response like this:
{"id":301,"name":"Pepo"}
If a user correctly inserted or if the search throws a good result. I have to go back in the browser to see the portal again.
How can I use AJAX to pass the following URL dynamically from the portlet to the server without refreshing the page afterwards?
http://localhost:8080/c/portal/json_service?serviceClassName=com.liferay.test.service.TrabajadorServiceUtil&serviceMethodName=findByName&servletContextName=TrabajadorPlugin-portlet&serviceParameters=[param1]&param1=NameInsertedByUser
Now I'm using the <form> tag like this:
<%
//Shows "New Employee" in the text field when portlet is rendered, or gets the user input and pass it as a param to the URL
PortletPreferences prefs = renderRequest.getPreferences();
String employee = (String)prefs.getValue("name", "New Employee");
%>
<form id="postForm" method="post" action="http://localhost:8080/c/portal/json_service">
<input name="serviceClassName" type="hidden" value="com.liferay.test.service.TrabajadorServiceUtil" />
<input name="serviceMethodName" type="hidden" value="create" />
<input name="servletContextName" type="hidden" value="TrabajadorPlugin-portlet" />
<input name="serviceParameters" type="hidden" value="[param]" />
<input name="param" type="text" value="<%=employee%>" />
<input type="submit" value="Submit"/>
</form>
I understand how AJAX works, but I need some help to create my function in order to achieve the URL to be correctly sent to the server for both GET and POST requests. This is my first try with AJAX.
Thank you very much, hope somebody understands my problem and could help me.
First of all, I see no point at all to use JSON services here. Just write ordinary portlet with MVC Controller, and in controller write action handling for corresponding actions (storing data, searching etc).
In controller you can directly call static methods like create or findByName from java class com.liferay.test.service.TrabajadorServiceUtil (or TrabajadorLocalServiceUtil) - that's how people usually do it.
If for some reason you really must use JSON, you should of course do these actions with AJAX calls - and render results using JavaScript.
Updating after question update:
The easiest and most correct way to send AJAX requests in Liferay would be to use AlloyUI JS framework that's a part of Liferay. You can read more on how to send AJAX requests with it here: http://www.liferay.com/web/nathan.cavanaugh/blog/-/blogs/4733559
In order to accomplish your goal I'd suggest implementing processAction(ActionRequest actRequest, ActionResponse actResponse) method in your controller/portlet.
In order to actually send data to it you'll have to have actionURL, which you can create using for example portlet:actionURL tag:
<portlet:actionURL /> or with Java code PortletURL actionUrl = portletResponse.createActionURL();
Then just submit your form using POST to this URL, and in actionRequest you'll have your parameters.

Explicit POST in JSP page

<FORM NAME="form1" METHOD="POST" action="some.jsp">
<INPUT TYPE="text" NAME="first" VALUE="First">
<INPUT TYPE="text" NAME="second" VALUE="Second">
</FORM>
When the form is submitted in the background a POST method is sent to that jsp page with the parameters.
What I am trying to do is that I have an ajax call to a local mediator jsp page which should then take those parameters and post to a page on another domain (this is for me to circumvent the cross-domain problem with ajax calls in IE8).
How would I do an explicit post? Something that takes a URL and the parameters?
If all you are having issue with is posting the form, it is as simple as
document.forms['form1'].submit()
EDIT: In that case, see Using java.net.URLConnection to fire and handle HTTP requests for how to make a POST or GET request. I would recommend using request.getParameterMap() and iterating over that, dropping those parameters into the new outbound request.
http://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/connector/Request.html

Resources