简体   繁体   English

PHP使用按钮和函数

[英]PHP Working with Buttons and Functions

I'm having difficulties on the executing a php function with a button 我在用按钮执行php函数时遇到困难

I've been browsing through the net (and ofcourse here at stackoverflow) for a solution to this problem. 我一直在网上(当然也是在stackoverflow上)浏览以找到解决此问题的方法。 However, still no luck. 但是,仍然没有运气。 This problem might have been asked many times in this site, but none of them seems to work to me (or am i just that poorly literate with programming). 这个问题可能在这个站点上已经被问过很多遍了,但是对我来说似乎都不起作用(或者我只是编程知识欠佳)。

What i am trying to achieve is to set a session variable once the button is clicked, and print it in the second page as it redirect. 我要实现的目的是在单击按钮后设置会话变量,并在重定向时在第二页中打印它。

here's what my first php page looks like: 这是我的第一个php页面的样子:

<?php 
session_start();
if(isset($_POST['submit'])) 
{
    $_SESSION['testing']= "hello world";
}
?>

<form method="post" action="update.php">
    <input type="submit" id="submit" name="sumbit" value="Submit" >
</form>

and here's the update.php: 这是update.php:

<?php 
session_start();
echo $_SESSION['testing'];
?>

It may look theres nothing wrong with it, but the script does not execute anything inside the " if(isset(... " statement. I am using WAMP as a server and my PHP version is 5.3.13 它看起来似乎没有什么问题,但是该脚本在“ if(isset(... ”)语句内未执行任何操作。我使用WAMP作为服务器,我的PHP版本是5.3.13

PS: I am aware that PHP is a server-side programming, and what i'm trying to do is something similar to client-side scripting. PS:我知道PHP是服务器端编程,而我想做的是类似于客户端脚本的事情。

The problem is, i do not know how to work with javascripting and what they call it "Ajax" scripting. 问题是,我不知道如何使用javascript以及他们所谓的“ Ajax”脚本。

Is there any way this could be fix? 有什么办法可以解决? Is it possible to do this with PHP alone without using javascript or ajax? 是否可以单独使用PHP而不使用javascript或ajax来做到这一点?

Hi simply empty action like <form method="post" action=""> on form tag and redirect after session setting.like header("location:update.php"); 嗨,您只需要在表单标签上添加<form method="post" action="">类的空操作,并在会话设置后重定向即可。header header("location:update.php"); .Hope solve your problem.Your first page code will be 希望能解决您的问题。您的首页代码将是

<?php 
session_start();

if(!empty($_POST)) 
{

    $_SESSION['testing']= "hello world";

    header("location:update.php");
}
?>

<form method="post" action="">

    <input type="submit" id="submit" name="sumbit" value="Submit">
</form>

While on update page 在更新页面上

<?php 
session_start();
echo $_SESSION['testing'];
?>
$.ajax({
    type: "get",
    url: "update.php",
    success: function(){
    alert("success");
    // do something
}
});

Above is the example of jquery ajax. 上面是jquery ajax的示例。

Try this one: 试试这个:

First Page: 第一页:

<form method="post" action="update.php">
    <input type="submit" id="submit" name="sumbit" value="Submit" >
</form>

Update.php: Update.php:

<?php 
session_start();
$_SESSION['testing'] = 'Hello World';

echo $_SESSION['testing'];
?>

Since you want to print the session in the second page, you should put a value of session in update.php not in your first page.. 由于要在第二页中打印会话,因此应将session的值放在update.php中而不是第一页中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM