Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm having this problem where I can't manage to accomplish this simple script. Basically, I want to take a post from a form, assign it to a variable, and then append the variable to a preexisting example.txt file. I apologize for the lack of examples, I switched to my phone after I went out to dinner with my inlaws. Could anyone post a simple example andd allow me to build off of that foundation? thanks in advance
The following should give you a foundation to start.
<?php
// Check to see if the form was submitted
if(isset($_POST['action']) && $_POST['action'] == 'add'){
$file = 'example.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $_POST['test'];
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
<form method="POST">
<!-- This becomes PHP variable $_POST['test'] -->
<input type="text" name="test" />
<input type="hidden" name="action" value="add" />
<input type="submit" value="Add"/>
</form>
Check out http://us3.php.net/file_put_contents for more information.
If your html form has a text field like this
<input type="text" name="firstName" value="Name">
you can access that value in your php script using
$_POST['firstName'];.
If you want to append text in php, you can simply do it using "."
Ex: $post_string = 'Ex:' . $post_string;
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
<form method="post">
Username: <input name="username" type="text" /><br />
Password: <input name="password" type="text" /><br />
Email: <input name="email" type="text" /><br />
Subject: <input name="subject" type="text" /><br />
<input type="submit" value="Submit" />
</form>
When someone fills out the form above, is it possible for the results to be uploaded to a text file, such as results.txt, rather than being emailed?
So for example, if the site link was hithere.com, the results would be uploaded to a password-protected text file at hithere.com/results.txt.
Thanks!
Here is a posible code to be executed from your .php form handler file
<?php
$myfile = fopen("result.txt", "a") or die("Unable to open file!");
$text = $_POST['username']."\n";
$text .= $_POST['password']."\n";
$text .= $_POST['email']."\n";
$text .= $_POST['subject']."\n\n";
fwrite($myfile, $text);
fclose($myfile);
?>
Also, you may need to add an action attribute to your form tag, whose value should be the same as the .php filename(or route if it is not in the same folder). For example if your php file is form_handler.php you should have <form method='post' action='form_handler.php'>
A little bit explaining on the php code above, you open the file with the second parameter "a" to tell the server you don't want to override the file's content but just add some content after the existing one(append). If you wanted to override the contetn that parameter should be "w" Then you set all the text in a variable, and write it in the file with fwrite(). After this you need to close the file with fclose()
Edit: In case you don't know, "\n" are linebreaks.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to create a page with php and put in text from form.
Here's what i know:
I know some php and how to write content to a database
I know how to create form in html
I need to send text from form createUserProfiles.php to /u/username.php using templates like /templates/profile-templates.php.
side note: i know how to create a page but i dont know how to write php code in it or use a template to create user profile. and i tried googling it but i didn't find anything that answered my question.
createUserProfiles.php
<form method="post" action="u/username.php">
<input type="text" name="username">
<input type="text" name="email">
<input type="submit" name="submit" value="Create User">
</form>
Now we will submit the data to database and will present these submitted values to u/username.php by loading the template templates/profile-templates.php inside it.
<?php
//connect to db
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
//feed the values from previous form to db
$stmt = $db->prepare("INSERT INTO table(username,email) VALUES(:uname,:email)");
$stmt->execute(array(':uname' => $_POST['username'], ':email' => $_POST['email']));
//now make an array with key name and its values
$attribute = array ('{user_name}'=>$_POST['username'], '{email}'=>$_POST['email']);
//load the template into a variable called $html
$html = file_get_contents ('templates/profile-templates.php');
//Now replace {user_name} and {email} present in template file with real data fetched from user.
foreach($attribute as $key => $value){
$html = str_replace ("{$key}", $value, $html);}
//At last echo the variable
echo $html;
Just make sure that the template file has curly brackets within which the data will be placed via PHP.
reference
Your method is hard you should use a sql server as data types and storage function are easy to use. Follow howCodes tutorials on creating a social network it will give you a basic idea of html forms and php
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In the following code, I would like to get the input, create the page, then go to the redirected page.
But instead I am getting the input, then going to the redirected page (it's skipping creating the page).
How can I achieve the first one?
<?php
$newfile = $_GET['abc'];
$file = 'example.com';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
<form onsubmit="location.href='http://www.example.com/file-created;">
<form name="form" action="" method="get">
<input type="text" name="abc" placeholder="Type Here. . .">
<input type="submit" value="Go">
</form>
</form>
I tried using echo, but it was of no use (maybe I am making a mistake), and when I input something (without redirect), my url changes to example.com?abc=[input_here]. Why?
When you don't have the onsubmit, it is submitting the form to the server to the same page that is currently loaded because you don't have a value for the action="" set. So if all of that code was in index.php, the form will by default submit to index.php because you didn't tell it go to anywhere else.
Since you are using method="get", it is going to add all the form data as a query string to the action (which you didn't specify, so it adds it to the current url). So it appends abc=(whatever input) to your url and submits the form.
Now, when you have the onsubmit event in the url, instead of submitting the data to the server, it is just redirecting to that url. Nothing is going to happen with all of the form data that was entered. It is basically thrown away.
What you need to do is have your code submit to your server, do whatever processing you need to do, and then redirect to the url you want.
So something like:
<?php
// check if "abc=(whatever)" is in the url, and if so do the copy to, otherwise skip it
if(isset($_GET["abc"])) {
$newfile = $_GET['abc'];
$file = 'example.com';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
} else {
header("Location: http://yoursite.com/" . $newfile);
exit;
}
}
?>
<form name="form" action="" method="get">
<input type="text" name="abc" placeholder="Type Here. . .">
<input type="submit" value="Go">
</form>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have a problem, i will send a POST with PHP but i will
send a GET Parameter with the form value:
<form action="/Eventsuche/" method="post">
<table>
<tr>
<td>
<input id="Headsucheort" name="Headsucheort" type="text" value="" />
</td>
<td>
<button type="submit" name="Submit" id="Headsuchestart" value="Headsuchestart">»</button>
</td>
</tr>
So, on submit he bring me to /Eventsuche/ but i would like to
go here: /Eventsuche/Value of Headsucheort
Thanks! :)
<button onclick="window.location.href='/Eventsuche/' + document.getElementById('Headsucheort').value">»</button>
Didn't test but
If you really must do it in PHP, add this to Eventsuch:
if ( isset($_POST['Headsucheort']) ) {
header('location:http://www.your-url.com/Eventsuche/'.$_POST['Headsucheort']);
exit;
}
I see two ways to the that.
first:
you can add your variable to the end of your action value such as: action="Eventsuche?var=somevalue"
and second one would be:
as a hidden input and even tho it is hidden, php will capture it on submit as in:
<input type="hidden" name="myvar_name" value="my_var_value" />
i think this should do it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Hello I am a beginner in php.
I've recently learned a bit about functions. But how do I dynamically insert data into the (). I've tried using get and post but that didn't work (Perhaps I just did something wrong). Any examples on how i could do it?
Thanks
action.php
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
<?php
function myFunction($argOne, $argTwo) {
echo "inside function argOne=".$argOne;
}
if (isset($_POST['name']) {
//call the function only if the form is submited
myFunction($_POST['name'], $_POST['age']);
}
?>
I think you want to pass some data to your function
The post just send the data to your php page.
If you want to send some data to your function you should do that as a parameter.
Example:
$myvar = $_POST;
dumpData($data){
var_dump($data);
}
dumpData($myvar);
//OR
dumpData($_POST);
You can't just send the post to your function, you need to call that and use it as a param.