onclick pass variables to ajax - html

AI have an expanding box, which contains some information, what I want to do and not sure if it is possible is that when somebody clicks on the link (shown below)
View Job Description
it passes a variable to Ajax and then have that update a mysql table to say the job description has been viewed.
Now I know the link above is just a link which triggers some javascript to expand the box and so just after some advice on how I can achieve this?.
I have other code which passes variables to ajax but through a form, and so I am not sure if this is possible.

Try this one using Jquery:
after the clicking the link the ajax request will be sent to the specified url.
ulr i.e. where your script will excute.
store all result in variable and print at the end of page.
Usins variable "data" you can get that result.
$(".expandLink").click(function(){
$.post("url",function(data,status){
$(div).html(data);
}
});

Related

CakePHP - refresh element/view cell using ajax

I'm trying to build a way to update a user profile one question at a time.
Essentially I want a div on my page that displays a form that lets the user submit the most important information, say firstname. When that has been filled out (success on the form) I want to refresh that div and show the second most important form, for say lastname, or if that is already filled in then birthday and so on.
Much like linkedin prompts you to add one more piece to your profile.
How would you do it?
My first thought was to use elements. But I need to fetch existing profile data to populate the form and how would the element get that data, abusing requestAction is not an option.
So I guess I in that case need to call a controller action that determines which element should be rendered, renders it to a variable and submits that (response->body) in json to the js that updates the page. Seems a bit .. unclean but should work.
Then we have view cells. They seem ideal for the task until I want to call them via ajax. That is not possible right?
So how would you go about to build something like that?
A form that needs to have the ability to be prepopulated with data if there is any and then refreshed automagically to display the form for the next piece of info needed.
View cells can't really be used in AJAX requests. This is not what they thought for.
The best thing you could do, if you want to keep the cell, is to make sure you followed SoC properly and put all your business logic into the model layer.
Now have a separate controller and action that is reachable from the outside throught a request (a cell is not) and return the data as JSON from there using the same code.
You could try to instantiate the cell in the controller action as well and send it's output. But honestly, I think that's a pretty fugly way of doing it.

The form data going to another page while action set to another page

I have a form in which the attribute action is set to go to a page but whenever i press the submit button its taking the data to another page.
while writing your form use post method if you are using get method or use get if using post. one of them uses to carry data while navigating to target.. so just change your form method..

Sending data to script from hyperlink

is there any way I can do something like in my example below but with normal hyperlinks ? I can't use drop down menu.
//edit: I have a web page and a script, on web page now i have drop down menu where user choose some values and submit them, script then do the work.
I just need to sent 3 values to script. (user must choose this values)
Hope its clearly now
this is part of code on my page:
<form action='script/script.py' method='post'>
<select name = 'menu0'>
<option value='x'>X</option>
<option value='y'>Y</option>
//
And also is there any easy way to remember what user set in dropdown menu ?
First, you can't use POST method in hyperlink, if your server code support GET method then you can create separate links for multiple options
x data
y data
....
.....
Other solution: This should also helpful you, as above create multiple links, you can hidden your form and submit form from <a> tag onclick event
With Hyperlink you can use GET method which is more easy to embed in the link like this :
X
Y
So make this work, you will be needing to change your script.py code, that make use of GET function instead of POST.
And easy way to remember what user set in dropdown menu is to store the input you GET via your script to a session variable when script.py gets executed. You can then use that session variable in whole scope of your application i.e. any other python scripts. Refer Session variables for this.

JQuery and Perl to display dynamic results on drop down selection

I'm currently using Perl CGI to display some internal billing pages at work. I'm using JQuery as well to add a little 'spunk' to it if you will.
I'm looking to generate/display HTML based on drop down selection by the user. Now, I can handle this part no problem. I'm generating table rows/table data on click based on the selection made by the user.
I want to retrieve and display values stored in the database for the corresponding data im displaying on button click, and without refreshing the page of course.
Can someone point me in the right direction? Thanks in advance.
You'll want to use an AJAX request to get the data. So, for example, if you have a select with class 'ajax' and you want to display data based on the option selected, you can do something like:
$('.ajax').change(function(){
$.get({'url', { value: this.val() }, function(data){
// display the data with a .html() or .append() or something
});
});
Replace 'url' with the url of a page that queries the database using the value that you send it.

Auto Refresh of a page

I have a web page consisting of a dropdown menu. What i want is the contents of other dropdown menus present in the page must change according to what has been selected in the first dropdown box. For example if a dropdown consists of Degree as its element. If i select Degree element then another dropdown must show only degree courses. This must happen automatically without clicking any button. How can i achieve this?
I would make this a comment, but I do not have commenting abilities. You are going to want to use $.post with jquery and ajax to accomplish this: http://api.jquery.com/jQuery.post/ . Ajax with JQuery is not difficult to do.
You will want to use the onchange event of the first drop down to use a jquery ajax $.post call to post to the server, passing the selection from the drop down as a parameter. The page that it post's to should us GET to get the selected attribute from the drop down, and then retrieve ( from the database or where ever) the options that go into the second drop down, based on the parameter. The code should be written out to the page. The jquery ajax call has a way to get the response and let you define a custom function to be run once the post returns. The return should be what you had written out to the page, and then you can just update the second drop down list with the returned data inside this function.
I hope this helps!