0

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?

1
  • 1
    if(isset($_POST['submit'])) is never executed because your first page isn't run with any post vars passed to it. You're just using that page to send a post request to update.php which doesn't do any such check Commented Mar 6, 2013 at 7:38

3 Answers 3

1

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'];
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Where your update.php located ? if in same directory it works otherwise you can set like localhost/project/update.php.
no, what i meant is, the redirect statement wont execute as well just like the "$_SESSION['testing']= "hello world";" statement.
it worked!, what i was looking for is that "IF (!empty..)" part! thank you this is a big help!
0
$.ajax({
    type: "get",
    url: "update.php",
    success: function(){
    alert("success");
    // do something
}
});

Above is the example of jquery ajax.

1 Comment

as i mentioned before i cant work with ajax, i dont know what this script even does,and how would it even call a php function?
0

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.