0

I'm just playing around with the form action and php. I'm not good at them, just starting to learn and recently I was playing around with the fopen, fwrite and fclose. Also playing around with basic form filling then php calls out what is filled.

What I'm wondering is, I know fopen, fwrite can be called to open/create a txt file and input simple data entered into the txt file but if I want to do that and also let the user see what they entered.

Not too good with explaining. Let's say for example

a.html --> let's say in this html

<form action="a.php" method="get">
<input type="text" name="name">
<input type="submit">

a.php -->after clicking submit in a.html this a.php will have the code of

echo "My name is $_GET["name"]";

which will show My name is xxxxx after clicking submit in a.html but what if I also want the submit in a.html to go into another let's say b.php and b.php will have the coding such as

$filehandle = fopen("text.txt","a+") or die ("sorry error");
fwrite($filehandle, $_GET["name"]);
fclose($filehandle);

I know if from a.html to go into just one of a.php or b.php then it will work but is it possible to make both a.php and b.php works with just one submit click on a.html?

don't want to involve javascripts and those more advanced because I'm not up to that level yet but I'm just thinking if there's a simple and possible way

Dora
  • 6,776
  • 14
  • 51
  • 99

4 Answers4

0

best to use javascript for this one. AFAIK, when you submit the form, it will load your target page passing the form variables specified in the form.

You can 1) use javascript to implement ajax to invoke your php scripts in your desired order

or 2)

on yourform target page, in the PHP invoke any additional PHP code you need to execute

Tucker
  • 7,017
  • 9
  • 37
  • 55
0

You could simply define two different functions for that.

function echoUsername($username) {
   echo "My name is $_GET["name"]";
}
function writeNameToFile($username) {
   $filehandle = fopen("text.txt","a+") or die ("sorry error");
   fwrite($filehandle, $_GET["name"]);
   fclose($filehandle);
}

Now just put these to a file called functions.php, include it into a.php/b.php using include("functions.php"); and call the functions like this:

echoUsername($username);
writeNameToFile($username);

If you really want to call both pages, there is no way around javascript but it's still going to be a bad solution.

Zulakis
  • 7,859
  • 10
  • 42
  • 67
  • ah, alright....I get what you mean now....let me try this later :P I know it's not the best way without using javascript but since I'm not at the javascript level yet and thought of such thing so curious if it is possible ^_^ – Dora Feb 14 '13 at 07:31
0

you need to include b.php in a.php like

<form action="a.php" method="get">
<input type="text" name="name">
<input type="submit">

In your a.php file you need to include

require_once "b.php";
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32
0

in a.php:

<?php
    $name = $_GET['name'];   // use post as the form method is better

    // then give it to **b.php**:

    header('location: b.php?name='.$name);

    // or

    echo "<a href='b.php?name='".$name.">Link to Page B</a>";
?>

You can get this value in b.php by:

<?php
      $new_name = $_GET['name'];   // get from the query string
?>
Tepken Vannkorn
  • 9,648
  • 14
  • 61
  • 86