English 中文(简体)
如何单击链接、 无线电按钮或复选框来改变 php 中变量的值
原标题:How to change value of a variable in php by clicking a link or radio button or checkbox
  • 时间:2012-05-25 15:54:07
  •  标签:
  • php

I am creating a system in php to get user s feedback on solutions. I am using MySQL to get the solution and store the feedback. The feedback are integers. There are two columns for feedback- Yes and No. I want to increase the value of them when a user clicks on a text link/radio button or checkbox. Like- .................Solution.............. Did this help you? Yes / No

When user clicks on Yes, it should increase value of yes column by one and if it clicks on No, it should increase value of no column by one. The main difficulty is this that the page contains numbers of solution and feedback options.. Can you help me??

问题回答

点击页面上的窗体控制是一个客户端动作。 在 PHP 中更新变量是一个服务器端动作。 幸运的是, AJAX 给了我们从客户端访问服务器资源的方法 。

以下代码使用 jQuery JavaScript 库执行 AJAX 请求 。 您不需要 jQuery 执行 AJAX 请求, 但随后您必须自行编码 AJAX 请求 。 这样做并不难, 但也不是微不足道 。

JavaScript :

$.ajax({
    url:  /path/to/file.php ,
    data:  url=encoded&query=string , // Can also be an object
    success: function( output ) {
        // This function gets called once the AJAX Request returns.  It is sent 
        // a string parameter containing all output of the Server-side script
    }
});

file.php 中,您只需执行任何您想要的行动。 AJAX 请求可以通过 GET 或 POST 发送,任何通过变量将分别在 $_GET $_POST 超级全球中提供。

output put 参数通过是一个字符串,它是由服务器脚本产生的全部输出(即 >echo / print )。该字符串可以是 > > > > JSON的演示词 ,在这种情况下,您必须 < a href=" "http://www.json.org/js.html" rel="nofolpol" 字符串 才能在 JavaScript 中使用它作为对象。如果您的服务器边脚本返回大量数据而不是普通的 HTML/XHTML,这将是有益的。

更多信息,請查看 jQuery s < a href="http://api.jquery.com/jQuery.ajax/"rel="nofollow" 用于 AAX 或 Google < a href="http://www.gogle.com/ search?q=jquery%20aax& ie=utf-8&aq==16- 8&aq=tvvvv;rvr="org.mozilla%3ax3- hax& org.mozilla%3a-us%3offical&s sps-ab=%20ax%20Reserps& oqq=20ax%20requex20requenations&aqf=20andamp;aq=g=10.0g=10g.0q.0q=15=15.

另一种办法是将您的控件作为正常的窗体元素,在按下时提交窗体(例如,每个控件都是一个命名的提交按钮/图像输入) 。您的窗体处理器将根据 $_POST 中存在的值来确定哪个控件被按。然后,该方法将把用户输入发送到一个在服务器上执行的窗体处理器,该处理器在完成处理后再将用户重定向到页面上。

PHP 仅是服务器侧面, 所以当用户点击“ 是 ” 时, 它需要发送要装入新页面、 Ajax 请求等请求, 这样会更新服务器。 页面加载后完成的任何事情都无法用 PHP 完成 。

我将在每个链接/无线电按钮上附上$_POST/$_GET的请求,然后将其发送到 MySQL的页面更新或类似内容上,以便更新您的表格。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签