Submit form data and go to next page on one click - php

Right now I have code that submits form data and gets put into a text file and then after I click a button to go to the next page. I want to be able to submit the form data and go to the next page on the same click. I'm using PHP and HTML right now. Is there anyway to successfully do this?? I tried an onclick like I have for my next button but it doesn't seem to work with the input type function.
<h2>Please enter your name.</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
First Name: <input type="text" name="fName">
<br><br>
Middle Initial: <input type="text" name="mInitial">
<br><br>
Last Name: <input type="text" name="lName">
<br><br>
<input type="submit" name="submit" value="Submit">
<button type="button" name="submit" value="Submit"
onclick="location.href='county.php'">Next</button>
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['submit']))
{
replace();
}
function replace() {
$myFile = "freewill.doc";
$fh = fopen($myFile, 'a') or die("can't open file");
$fName = $_POST["fName"];
$mInitial = $_POST["mInitial"];
$lName = $_POST["lName"];
$placeholders = array('fin', 'min', 'lana');
$namevals = array($fName,$mInitial,$lName);
$path_to_file = 'freewill.doc';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($placeholders,$namevals,$file_contents);
file_put_contents($path_to_file,$file_contents);
fclose($fh);
}
?>

Change below line
<button type="button" name="submit" value="Submit"
onclick="location.href='county.php'">Next</button>
to
<input type="submit" name="submitform" value="Submit">Next</button>
and change PHP code to this
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['submitform']))
{
$myFile = "freewill.doc";
$fh = fopen($myFile, 'a') or die("can't open file");
$fName = $_POST["fName"];
$mInitial = $_POST["mInitial"];
$lName = $_POST["lName"];
$placeholders = array('fin', 'min', 'lana');
$namevals = array($fName,$mInitial,$lName);
$path_to_file = 'freewill.doc';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($placeholders,$namevals,$file_contents);
file_put_contents($path_to_file,$file_contents);
fclose($fh);
header('Location: county.php');
}

Try setting an onSubmit method for your form instead.

Related

Posts are duplicating in my php page when refreshing site

I cannot make my post system work because posts in comment section duplicate if I refresh the page
I am using only HTML and PHP. It's a forum for me and my friends.
There is also code above this but it is unimportant
<form action="" method="POST">
<label> Topic:
<input type="text" name="Topic" class="Input" style="width: 300px" required>
</label>
<br><br>
<label> Name:
<input type="text" name="Name" class="Input" style="width: 225px" required>
</label>
<br><br>
<label> Comment: <br>
<textarea name="Comment" class="Input" style="width: 300px" required></textarea>
</label>
<br><br>
<input type="Submit" name="Submit" value="Submit" class="Submit">
<!--idk-->
</form>
</center>
<hr>
<br>
</body>
<!--posts-->
</html>
<html>
<center>
</html>
<?php
if($_POST['Submit']){
print "<h1>Your comment has been submitted!</h1>";
}
?>
<html>
</center>
</html>
<?php
$Topic = $_POST['Topic'];
$Name = $_POST['Name'];
$Comment = $_POST['Comment'];
#Get old comments
$old = fopen("comments.txt", "r+t");
$old_comments = fread($old, 1024);
#Delete everything, write down new and old comments
$write = fopen("comments.txt", "w+");
$string = "<b>".$Topic."</b><br>".$Name."</b><br>".$Comment."</br>\n".$old_comments;
fwrite($write, $string);
fclose($write);
fclose($old);
#Read comments
$read = fopen("comments.txt", "r+t");
echo "<br><br>Comments<hr>".fread($read, 1024);
fclose($read);
?>
The problem you are facing is due to the way you are appending the comments to the "comments.txt" text file. The problem is that every time a comment is sent, all old and new comments are written to a text file. So when you refresh the page, the same comment is repeated.
Hope the following code helps
<?php
$Topic = $_POST['Topic'];
$Name = $_POST['Name'];
$Comment = $_POST['Comment'];
# Write the new comment to the top of the file
$write = fopen("comments.txt", "a+");
$string = "<b>".$Topic."</b><br>".$Name."</b><br>".$Comment."</br>\n";
fwrite($write, $string);
fclose($write);
# Read comments from the file
$read = fopen("comments.txt", "r");
echo "<br><br>Comments<hr>";
while(!feof($read)){
$line = fgets($read);
echo $line."<br>";
}
fclose($read);
?>

form outcome getting used as a variable in a different file

im working on a website where i want to take the outcome from a from and want them saved in a seperate file as php variables so i can read them out later on a diffrent part of the site. i can write some php code to a file but i writes the name of the variable and not the value.
<form action="change.php" method="post">
<input type="text" class="form-control" id="inputlocation1" placeholder="<?php include 'data1.php'; echo $location1 ?>">
<br>
<input type="text" class="form-control" id="inputdate1" placeholder="<?php include 'data1.php'; echo $date1?>">
<br>
<textarea name="description1" class="form-control"></textarea>
<br>
<input type="submit" name="submit" value="update data" class="btn btn-primary">
i use numbers behind the id's because i have multiple dates and locations i want to change. this is my current php code:
<?php
$file = fopen("data1.php", "w") or die("Unable to open file!");
$date = $_POST["inputdate1"];
$loc = $_POST["inputlocation1"];
$desc = $_POST["description1"];
fwrite($file, '<?php $loc1 = $loc' );
fwrite($file, '$date1 = $date');
fwrite($file, '$desc1 = $desc');
fwrite($file, '?>');
fclose($file);
?>
who can help me?
$data = '$desc1 =' . $desc;
fwrite($file, $data);
you must use SIGLE QUOTES

how i can call the button inside the if statement PHP

I am a newbie I am having a little problem on how can i call the button inside my if statement. I am trying to work on a userlog where when you click a button it will output on the user log.
but how can i call the >>
THE VIEW button inside my if statement so that it will fwrite inside the userlog.txt.
inside this if($_POST['submit'] = ") same goes in the search button and the add edit delete button. did u get my point?
<body background="images.jpg">
<?php
session_start();
if($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a><br>";
echo '<FORM METHOD="LINK" ACTION="mydata4.php">
<INPUT TYPE="submit" VALUE="Edit/Delete/add">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="mydata2.php">
<INPUT TYPE="submit" VALUE="View" id="viewbutton">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="display_data.php">
<INPUT TYPE="submit" VALUE="Search">
</FORM>';
}
else
{
die("You must be logged in!");
}
if($_POST['submit'] = ")
{
$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username click view button";
fwrite($fh, "$stringData".PHP_EOL);
fclose($fh);
}
?>
Use
if(isset($_POST))
instead of
if($_POST['submit'] = ")
try this... you are doing mistake in your if condition.
<body background="images.jpg">
<?php
session_start();
if($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a><br>";
echo '<FORM METHOD="LINK" ACTION="mydata4.php">
<INPUT TYPE="submit" VALUE="Edit/Delete/add">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="mydata2.php">
<INPUT TYPE="submit" VALUE="View" id="viewbutton">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="display_data.php">
<INPUT TYPE="submit" VALUE="Search" name="submit" id="submit">
</FORM>';
}
else
{
die("You must be logged in!");
}
if($_POST['submit'] == "submit")
{
$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username click view button";
fwrite($fh, "$stringData".PHP_EOL);
fclose($fh);
}
?>
you can set the same name to your submit buttons ...
<input type="submit" name="submit" value="View" />
<input type="submit" name="submit" value="Search" />
and in your PHP, you can simply use as you mentioned
if( $_POST["submit"] == "View" )
...
if( $_POST["submit"] == "Search" )
...
one more things is that, you have to use method="post" in your <form> tag if you want to use $_POST in php
<form method="post" action="....php">

save php output as a new html file

I worked out a form to fill out on a simple php site, how can I set up a script to put that information in a html/php file and finally save it on the server (file name should be the same name as the $name)
$name = $email = $friendcode = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$friendcode = test_input($_POST["friendcode"]);
}
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name"><?php echo $nameErr;?>
<br><br>
E-mail: <input type="text" name="email"><?php echo $emailErr;?>
<br><br>
Friendcode: <input type="text" name="friendcode"><?php echo $friendcodeErr;?>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
You can use the fopen(), fwrite() and fclose() functions provided by PHP.
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$friendcode = test_input($_POST["friendcode"]);
$file=fopen($name,"a") or die("can't open file");
fwrite($file,$email." ".$friendcode);
fclose($file);
}
This will result in a simple text file, but you can also fwrite() any HTML/PHP code you like.
Try to create a file using fopen,
$save_str = '<p>Name :'.$name.'<br/>e-mail:'.$email.'<br/>friend code'.$friendcode.'</p>';
$handle = fopen($name.'.html','c');
fwrite ($handle, $save_str);
fclose ($handle);
more about fwrite

PHP script doesn't save text files on the server

This is the PHP script I wrote in a folder of my server:
<?php
$answer = $_POST['ans'];
if ($answer == "yes.") {
$testo = "qwe\r\n";
$documento ="/other/yes.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
else {
$testo = "qwe\r\n";
$documento ="/other/no.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
fclose($identificatore);
header('http://mk7vrlist.altervista.org/other/poll.html');
?>
This code takes the answer from a radiogroup and if the answer is Yes, it makes a file called yes.txt. Else it makes a file called no.txt. This is the image.
This is the HTML code:
<form name="mainform" action="poll.php" method="POST">
<br />
<input type="radio" name="yes" value="yes">Yes, I like it.<br>
<br />
<input type="radio" name="yes" value="no" onclick="apri();">No, I want the old layout.
</fieldset>
//other code...
<input type="submit" value="Send" />
</td>
</table>
</form>
When I click the "Send" button, the script doesn't save anything on the server. Do you know why?
One problem is you are using $_POST['ans'] but your actual radio button is called 'yes'.
Also there's a stray closing fieldset tag in your form.
First both your radio buttons should be called name="ans" and not name="yes"
Your if ($answer == "yes.") should not have a dot in it.
And your header is not properly formatted:
header('http://mk7vrlist.altervista.org/other/poll.html');
should read as:
header('Location: http://mk7vrlist.altervista.org/other/poll.html');
More on headers on PHP.net: http://php.net/manual/en/function.header.php
This:
<input type="radio" name="yes" value="no" onclick="apri();">
onclick="apri(); does not belong in there. If you're going to use this, would be used in conjunction with a submit button.
This $testo = "qwe\r\n"; may need to be reformatted as $testo = "qwe" . "\n"; Linux and Windows react differently using \r
HTML form (reformatted)
<form name="mainform" action="poll.php" method="POST">
<br />
<input type="radio" name="ans" value="yes">Yes, I like it.<br>
<br />
<input type="radio" name="ans" value="no">No, I want the old layout.
</fieldset>
<input type="submit" value="Send" />
</td>
</table>
</form>
PHP handler
<?php
$answer = $_POST['ans'];
if ($answer == "yes") {
$testo = "YES\n";
$documento ="yes.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
else {
$testo = "NO\n";
$documento ="no.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
fclose($identificatore);
header('Location: http://mk7vrlist.altervista.org/other/poll.html');
// echo "ok done";
?>
EDIT (optional as a counter method)
You may also want to use your answers as a counter instead of appending data which will eventually could grow very large.
NOTE: You must first create two files with the number 0 (zero) inside them.
yes_count.txt and no_count.txt
Here is a tested example of such a way:
<?php
$answer = $_POST['ans'];
if ($answer == "yes") {
$fr_yes = fopen("yes_count.txt", "r");
$text_yes = fread($fr_yes, filesize("yes_count.txt"));
$fw = fopen("yes_count.txt", "w");
$text_yes++;
fwrite($fw, $text_yes);
// will echo the counter but you can use header to redirect after
echo $text_yes;
// header('Location: http://mk7vrlist.altervista.org/other/poll.html');
}
else {
$fr_no = fopen("no_count.txt", "r");
$text_no = fread($fr_no, filesize("no_count.txt"));
$fw = fopen("no_count.txt", "w");
$text_no++;
fwrite($fw, $text_no);
// will echo the counter but you can use header to redirect after
echo $text_no;
// header('Location: http://mk7vrlist.altervista.org/other/poll.html');
}
?>
For debugging purposes, you should use a die statement when trying to fopen.
$fh = fopen($documento, 'a') or die('Could not append to file');
and if error reporting is on, you can always use error_get_last() to get a more detailed description of what's failing.
you are trying to write in / (root directory)
php user have permissions over the folder?
try this:
/project_folder
-poll.php
-other (create other folder with write permissions).
poll.php
<?php
$base = __DIR__; //if your php version dont work __DIR__ try: dirname(__FILE__);
if ($answer == "yes.") {
$testo = "qwe\r\n";
$documento =$base."/other/yes.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
else {
$testo = "qwe\r\n";
$documento =$base."/other/no.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
fclose($identificatore);
header('http://mk7vrlist.altervista.org/other/poll.html');

Categories