views:

49

answers:

2

Hello,

I have a table in my php code. In the second column of the table, normally it displays "---", and after i click the submit button, it will display the calculating result in the same page.

For example,

<table>
<tr><td>1</td><td>---</td></tr>
<tr><td>2</td><td>---</td></tr>
<tr><td>3</td><td>---</td></tr>
</table>

And in the same page, it should receive the variables

$var1 = $_POST['variable1'];

I'm not sure which way to use, ajax or javascript or just css?

Do you have any ideas?

Thanks

+1  A: 

You can use Javascript if you want to use the calculation on the client side, PHP if you want it on the server side. From your question I guess you want it on the server side.

Just submit the form to the same page: action = "your_page.php". Test if the page is submitted via your form, then display the results (a table with the results), after you calculate them, of course. Else display the page like it is now.

With JavaScript you can use DOM to select that exact td where you want your results and display them there, after you calculate them via JavaScript.

A third option.. ajax, has the advantage that can calculate the results in real time (without pressing the submit button).

You must decide what you want before using one of the methods, each one of them has some advantages and some disadvantages.

vladv
Thanks, i treated the data including a php page. Just your second solution. It works and thanks.
garcon1986
I also found, the values are lost when i refresh the page. Do i need to use ajax finally?
garcon1986
Not sure if I get you right here:Do you want sticky forms? (Check them out on google, there are many examples) Or you want a user to complete the form only once then displayed the results when he visits:It is normal to lose the info if you refresh the page. That will happen with ajax also. You will get the information in real time but when the page is refreshed you will have the original page. There are ways to know if the user visited that page but they are not really deterministic. For example you can save a cookie.
vladv
I want to get the parameters using the form and when i click the submit button, it will save the parameters i have chosen and display the result in a table in the same page. Right now, i use form action="thesamepage.php", and when i submit, it will display but the parameters i have choosed are lost. What can i do? Do i need stciky forms?
garcon1986
yes. you need sticky forms. Output in the value field of your form fields the $POST['whatever'].
vladv
yes, i have used it. and for the drop down list with images, i have tried to make it work when refreshing the page. Do you have any ideas? http://stackoverflow.com/questions/2921607/how-to-change-picture-using-drop-down-list
garcon1986
A: 

you could achieve this with just php and css but ajax would make it slick. i'd use JSON to parse data from a .js to a .php and back. jQuery makes this really easy (using $.post)

$(document).ready(function() {
$.post('calculate.php', { value: $('td') }, //posts value to php
 function(data) {
  if(data.success) {
   alert(data.result); //gets the value $result from php
  }
 else {
  alert('error');
 }
}, 'json');
});

this would post the value of td to calculate.php, which could calculate and echo out JSON data

$value = $_POST['value'];//assign value from js to $value
.....
$data['success'] = true; //there was no error
$data['result'] = $result; //where $result is calculated within php
echo json_encode($data);

you could pass as many variables between the js and php files. you could even animate your results.

these links might help

http://davidwalsh.name/animated-ajax-jquery

http://www.9lessons.info/2009/08/jquery-and-ajax-best-demos-part-3.html

http://www.noupe.com/ajax/30-fresh-ajax-tutorials-and-techniques.html

pixeltocode
Thanks, but i think it's a little heavyweight. I prefer to treate it with php in server side.
garcon1986