Hello everyone,

I'm just wondering if there is another of calling functions in a different php file without making the app run strangley. So what I have got are a few forms without specifying their action.

<?php
   1- checking for the fields 
   2-  if they are not empty and so on, a function in functions.php will be called then users will be redirected from this page not  from  the functions.php because I am using some functions in different ways.
?>


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

fields stuff
</form>

when I redirect users from login.php to index.php, it adds a hash at the end of the url. E.g localhost/login.php#index.php or something like this.

Is there a way I can solve this easily without specifying action for forms? or specify action to trigger the right function maybe?

Recommended Answers

All 6 Replies

Am I correct in saying that after you validate youre form you want to run function x from a different php file ??

Why dont you just do an include on the other php file ??
then you can access its functions

Yeah, you're correct. I have included the file containing the functions.But in the form attribute 'action', I left it empty. I've just realised the first post wasn't so sensible. OK. I worte it really fast because I was in a hurry.

well I actually found this, although its slightly different setup but still functions the way you would want. So it has a hidden input which then determines whether or not the form has been clicked on and only then it actually runs the php code.This will run the php code that is in the file itself, did you want to do that or call a function from a seperate script, upon sbmitting the form ??

<?PHP
     include 'xxxx';

    if(isset($_POST['form']) && $_POST['form']=='true')
    {
    //in here you can then call function() form other php file;
    }
    ?>

    <form>
        <input name="username" type="text" id="username" />
        <input name="password" type="text" id="password" />
        <input name="form" type="hidden" id="form" value="true">
        <input type="submit" name="Submit" value="Submit" />
    </form>

I wanted to call a function from the xxx.php which let's assume has these functions:

function check_user($username, $password){
}

function delete(){
}

function log_out(){
}

None of these functions includes the header function to redirect users. So in my login.php after the valdiation is successful, users will be redirected to index.php

inside my login.php, I have something not simple like this though :)

<?php
include("xxx.php");

 if(isset($username, $password){
     check_user($username,$password);
     header("Location:index.php");
     die();
 }
?>

<form action="" method="POST">
   username
   password
</form>

make sense?

The problem is that it concatnates the url from header function to the current url. So you're on login.php, it will add #index.php => login.php?#index.php

@sss93... Does that make sense?

The empty form action will leave the action as the current page, like using $_SERVER['PHP_SELF'] and will make no difference to header("Location:index.php"). I replicated your setup:

<form name="form" method="post" action="">
<input name="submit" type="submit" value="submit"><form>

on one page, and

<?php if(isset($_POST)){ header("Location:newpage.php"); die(); } ?>

on the included page, and it redirected without any concatenation. Are you using a variable as the redirect location for the header function?

header("Location:".$redirect_page);

If so, I think the concatenation takes place there. If I have misunderstood, then please excuse me :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.