submit should not redirect to another page in HTML form

Maniv G

I have a HTML form which reads some data and saves in the text file with help of PHP. The form's HTML code looks like below.

What is happening now:
1. once i click on submit button it redirects to the 'myprocessing.php'
2. successfully saving in data.txt

The help i required on
1. when i click on submit it shouldn't redirect me to the php page, it should stay on the HTML form page
2. it should show the php file's output on the same HTML page
In simple words, I want to stay on the HTML page itself. Since I'm pretty new to HTML and PHP, struggling much to do these. Thanks in advance :-)

<html>
<head>
<title></title>
</head>
<body>
     <form action="myprocessing.php" method="POST">
     <input name="field1" type="text" />
     <input name="field2" type="text" />
     <input type="submit" name="submit" value="Save Data">
     </form>
<a href='data.txt'>Show data</a>
</body>
</html>

This is my data prcoessing PHP file.

<?php
echo "starting...";
if(isset($_POST['myTextBox']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
    $ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
    die('There was an error writing this file');
}
else {
    echo "$ret bytes written to file";
}
echo "Ended...";
}
else {
die('no post data to process');
}
?>
rack_nilesh

Write php code in same html file and use isset() function to check for $_POST data

Try this

<?php
if(isset($_POST['field1']) && isset($_POST['field2']))
{
echo "starting...";
if(isset($_POST['myTextBox']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
    $ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
    die('There was an error writing this file');
}
else {
    echo "$ret bytes written to file";
}
echo "Ended...";
}
else {
die('no post data to process');
}
}
?>

<html>
<head>
<title></title>
</head>
<body>
     <form action="current_page.php" method="POST">
     <input name="field1" type="text" />
     <input name="field2" type="text" />
     <input type="submit" name="submit" value="Save Data">
     </form>
<a href='data.txt'>Show data</a>
</body>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redirect to another page after form submit

Django Ajax Form submit wrongly redirect to another page

How do I redirect to another page on form submit?

After submit redirect to another page

Redirect on another html page

Submit form POST to another page but validate before redirect and keep on same page if validation fails

How to redirect to another HTML page after submitting a form?

Form submit from mailer.php goes to blank page. Possibly redirect to previous html page?

Redirect to home page after submit form vue

form submit and redirect to same page with no re render

Django 2 Redirect to next page on form submit

Redirect to anchor in same page after form submit

yet another redirect after form submit

How to redirect to another page after click on submit button in form with required inputs

How can I on Submit my Form redirect the Results to another Page in React

Pass data to another page and submit form on that page

Redirect to page in form submit and use the form data in new page

html form submit calls a php but does not redirect

How to submit a form results to a table on another page?

Submit form from another page with Javascript

On form submit, open a new page in a new tab and redirect the original page

Submit form without redirect to action page, but refresh current page

Redirect from an HTML page to another page to a title

Redirect another page after successful form submission

redirect to another page after submitting a form

A button who submits a form and redirect to another page

How to redirect to another view/page if the form is invalid

How to open pop up modal when form submit sucessfully and hide pop after 2 mintues and redirect to another page in react js?

Prevent HTML form submit from refreshing page and triggering another function instead