在同一页面显示计算结果

发布于 2024-09-03 16:08:29 字数 468 浏览 4 评论 0原文

我的 php 代码中有一个表。在表格的第二列中,通常显示“---”,点击提交按钮后,会在同一页面显示计算结果。

例如,

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

在同一页面中,它应该接收变量

$var1 = $_POST['variable1'];

我不确定使用哪种方式,ajax 或 javascript 还是只是 css?

你有什么想法吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2024-09-10 16:08:29

如果你想在客户端使用计算,你可以使用Javascript,如果你想在服务器端使用计算,你可以使用PHP。从你的问题来看,我猜你希望它在服务器端。

只需将表单提交到同一页面:action = "your_page.php"
测试页面是否通过表单提交,然后显示结果(包含结果的表格),当然是在计算结果之后。否则像现在一样显示页面。

通过 JavaScript,您可以使用 DOM 选择您想要结果的确切 td,并在通过 JavaScript 计算结果后将其显示在那里。

第三种选择.. ajax,其优点是可以实时计算结果(无需按提交按钮)。

在使用其中一种方法之前,您必须决定自己想要什么,每种方法都有一些优点和一些缺点。

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.

三岁铭 2024-09-10 16:08:29

你可以只用 php 和 css 来实现这一点,但 ajax 会让它变得光滑。我会使用 JSON 将数据从 .js 解析到 .php 并返回。 jQuery 使这变得非常简单(使用 $.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');
});

这会将 td 的值发布到calculate.php,它可以计算并回显 JSON 数据,

$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);

您可以在 js 和 php 文件之间传递尽可能多的变量。您甚至可以将结果动画化。

这些链接可能会有所帮助

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文