tags:

views:

73

answers:

4

I have a entry form. Below it, I want to show a grid containing existing records. As the user clicks on a row of the grid, the values must get filled in the fields of the form above.

Is there any way to do this without refreshing the page?

A: 

maybe you should use javascript?

adek
That's not really a sufficient answer, is it?
Franz
@adek: I don't think it is possible with only JavaScript.
RPK
Yes it is. When your grid stores all needed data to fill the inputs of the form. The only thing is to identify the value (example <td id="value1">value</td> and find <input id="input-value1"/>). You can use JQuery for it.
adek
@adek: Interesting. Can you provide any link that I can refer?
RPK
A: 

Go get JQuery, and take a look at its AJAX functions. You will need to prepare the data in PHP, and create a special page to serve them. The AJAX function will be called when the user clicks on the button, it will load the data from the server, then it will parse them, and fill the grid...

Palantir
+1  A: 

If all the info the form needs is already present on the page then AJAX would be overkill and simple Javascript would do the job perfectly. This isn't tested, but;

<form id="myform">
  <input id="name" type="text" ... />
</form>

<tr> 
   <td onclick="populateFormElement('myform', 'name', this);">Bob</td>
</tr>

in javascript;

function populateFormElement(formName, inputName, element)
{
   document.formName.inputName.value = element.firstChild.data;
}

This works on a single element but should be a reasonable starting point as the process is easy to adapt to whole rows. Simply put the onclick event on the tr element and step through the various children a bit more to extract the relevant data.

MatW
I really don't know, that's why I am asking. No criticism intended. Can the "user data" (the stuff in the table for example) really be trusted? Would it even be bad, if the user changed something?
Franz
No problem. I realise you don't know, but whether that function worked on an individual element or whole group of them (ie a table row), you'd need to understand what was happening to make it work in your specific application. Maybe your next stop should be a Javascript tutorial or two? As for whether the user data can be trusted, I can't tell without a lot more info. I'd say that was a whole new question!
MatW
+1  A: 

Is there any way to do this without refreshing the page?

So, the key is asynchronous. The first A in Ajax stands for the very same word. You also tagged the question with Ajax. So I'd expect that you have certain knowledge about that.

But the actual question doesn't require an ajaxical solution at all. You don't need to send and receive data from the server side at all, simply because all the needed data is already available inside the same HTML DOM tree.

The only feasible language which can access and traverse the HTML DOM tree at the client side is Javascript. It does not refresh the page at all. Just write code accordingly and execute it during the onclick event of the table row.

If you actually have a problem in writing the Javascript code accordingly, then you need to elaborate a bit more about it in your question. Do not ask "Is there any way?" --the answer is in 99.99% of the cases yes, but just ask "How do I do it?", supplied with relevant information about the coding you have as far.

BalusC
@Balus: I am using a third-party DataGrid to show the records. I need to experiment whether column values are fetchable through DOM. The DataGrid is designed in such a way that when a row is clicked, it picks the rowID and then searches for more details on the server.
RPK
That "third-party" DataGrid (let me guess, ASP?) undoubtely just renders nothing else than a heap of plain vanilla HTML/JS/CSS. Open the page in your webbrowser and choose 'View Source'. A new world will open.
BalusC