Reputation: 33
I'm having difficulties on the executing a php function with a button
I've been browsing through the net (and ofcourse here at stackoverflow) for a solution to this problem. 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
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:
<?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
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.
The problem is, i do not know how to work with javascripting and what they call it "Ajax" scripting.
Is there any way this could be fix? Is it possible to do this with PHP alone without using javascript or ajax?
Upvotes: 0
Views: 301
Reputation: 4331
Hi simply empty action like <form method="post" action="">
on form tag and redirect after session setting.like 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'];
?>
Upvotes: 1
Reputation: 1629
Try this one:
First Page:
<form method="post" action="update.php">
<input type="submit" id="submit" name="sumbit" value="Submit" >
</form>
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..
Upvotes: 0
Reputation: 1629
$.ajax({
type: "get",
url: "update.php",
success: function(){
alert("success");
// do something
}
});
Above is the example of jquery ajax.
Upvotes: 0