I have been trying to make a simple suggestions box
HTML from .html page
<form action="suggestions.php">
Suggest some content for this page: <input type="text" name="suggestion"></br>
</br>
<input type="submit">
</form>
PHP from suggestions.php page
<?php
$filename = date('Format String');
$Content = $_POST["suggestion"];
file_put_contents('./suggestions/'.$filename, $Content);
?>
What it should do
Allow anonymous input of suggestions
Create file with suggestions in "suggestions" directory
Name file as date and time to the second
The default form method is GET. You need method="POST" in your form tag, like so:
<form action="suggestions.php" method="POST">
Suggest some content for this page: <input type="text" name="suggestion"><br>
Related
i have a form having employeeNo,name and a submit button. when the form is submitted it redirects to a php page, Now the problem is i want to access the values of the form by using 'id' attribute?if possible i need the html and php code.
Is there a way to solve this Problem?
enter code here<html>
<head>
</head>
<body>
<form action="test.php" method="">
<input type="text" name='employeeNo' id="input1">
<br>
<input type="text" name='name' id="input2">
<br>
<input type="submit" id="input3">
</form>
</body></html>
In your test.php file you need to retrieve the values. Since you don't define any method in your form, by default it should be "GET"
Your php file could look like the following:
<?php
$employee_no = $_GET['employeeNo'];
$name = $_GET['name'];
I would suggest defining your form method as POST though, like the following:
...
<form action="test.php" method="POST">
...
so your php file will retrieve values from form like this:
<?php
$employee_no = $_POST['employeeNo'];
$name = $_POST['name'];
You may also look at this for further explanation:
https://www.w3schools.com/php/php_forms.asp
Edit: Just to clarify, I only want to open a file and change some text.
I have a basic HTML page with a form, and I want to change the POST action programatically with PHP.
I have this PHP script which I got from another post:
<?php
$file = file_get_contents($argv[1]);
$startPoint='action="';
$endPoint='"';
$newText='phpfile.php';
$newFile = fopen($argv[1], "w");
fwrite(
$newFile,
preg_replace(
'#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si',
'$1'.$newText.'$3',
$file
)
);
fclose($newFile);
?>
..and this HTML file:
<html>
<form method="POST" action="https://www.example.com/">
<input type="text" name="email">
<input type="password" name="password">
<input type="submit" name="button">
</form>
</html>
this does replace example.com with phpfile.php, but removes other lines of the HTML. This is what I'm left with after running the PHP script:
<html>
<form method="POST" action="phpfile.php">
</form>
</html>
I haven't been programming in PHP for long and some help would be appreciated.
First of all
Bad idea on replacing strings in files. A good way to approach this is having a php file with a variable which you could programatically define as the action file to be posted at.
Anyway
As that was not the question, I don't know what those # were supposed to mean, but in PCRE(php regex parser), here is the fixed code :
preg_replace(
'/('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')/i',
'$1'.$newText.'$3',
$file
)
I'll preface this with I'm not a coder nor aspiring to become one.
I just want to play around with something simple.
Please don't feel bad about spoon-feeding me here haha.
All I want is when I hit a my submit button the text entered in the text field is saved to a file called log.text
I want it to overwrite each time.
Once data has been written I want it to redirect to another page.
Tried this but it doesn't create the file nor write to it even if I create it manually. The redirect also doesn't work because I'm an idiot.
Any help guys? :(
<?php
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
?>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
It is because the action element of the form is empty.
It should be \n
<form action="action.php(or any other php file that is handling the form)" method="post" name="form">
This guide offered me the solution I was after.
Thanks anyway guys!
http://www.howtoplaza.com/save-web-form-data-text-file
Because you dint checked whether the form is submitted or not. if submited create log. code given below
<?php
if(isset($_REQUEST['submit']))// if to check whether submit name is passed or not
{
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
}
?>
<html>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
</html>
It is very difficult for me to put in words my query. But I will try.
I have a site xyz.com which has search facility for listed products. The search page url is generated like this :www.wyz.com/search/search_term
I want to create a iframe page in a third party site with a search facility which can directly communicated with my site xyz.com.
I have tried to create a search box with a submit button. I want to append the search query in as a variable to my form action url string.
So the search string should look like this :www.wyz.com/search/my_string_variable
The code I have written is:
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
}
?>
<?php
$result=$url.$r1
?>
<html><body>
<form action="<?php echo $result; ?>" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
==================================================================
But output what I get, is only "http://www.xyz.com/search/". It removes my variable from the url. I am not able to find what is the reason? I have also tried to print result via to check the actual output and it shows that it has added the value at the end of url. But when I want to achieve the same thing via form action it does not work. please help?
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
$result=$url.$r1;
header("location:$result");
}
?>
<html><body>
<form action="" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
Please try the above code. I have made some modifications. The main reason your code is not working is whenever you press the submit button it is going to the the url "http://www.xyz.com/search/" directly .The if condition is never executed. In the above mentioned code it will work properly
action="" - you are submitting to the wrong url. Here is alternate version -
<?php $url='http://www.xyz.com/search/';
if (isset($_POST['submit'])) {
$r1=$_POST['num1']; header("Location: ".$r1); // 302 redirection
}
?>
<html><body> <form target="_SELF" method="post"> Num1:<input name="num1" type="text" /><br /> <input type="submit" name="submit" /> </form> </body></html>
Is this possible? I have looked online and cannot seem to find an answer.
yes why not
sure you can post to the same page :)
INDEX.PHP
<?php
if (isset($_POST['txt']))
echo 'Thnx for submitting the form';
else
echo 'Submit the form';
?>
<form name="frm" method="post" action="index.php">
<input type="text" name="txt" id="txt"/>
<input type="submit" />
</form>
yes,
<form name="name" method="post" action="">
then run the php to handle the data in the top of index.php
This not exactly what you asked for but this will make your form submit to its own page without reloading the page so here is the link. It's a youtube video.
http://www.youtube.com/watch?v=FPXL-ZU3rEY&feature=related