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
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to make a private message system where users can have conversations with each other. Each conversation has its unique hash. I want to store the messages in my database but for some reason the value of hash gets stored as 0 instead of the number I am requesting.
<form method="post" action="exiConversations.php" id="sendMessageFooterForm">
<input type="text" name="userNewMessage" id="userMessage" placeholder="Type een bericht" />
<input type="submit" name="sendNewMessageSubmit" id="sendMessageSubmit" value="Verzend" />
<?php
if (isset($_POST['sendNewMessageSubmit'])) {
$message = $_POST['userNewMessage'];
$fromUser = $_SESSION['userID'];
$today = date("y/m/d H:i:s");
$exiHash = $_GET['hash'];
$insertNewMessage = $conn->query("INSERT INTO messages (fromuser, messagedate, message, grouphash) VALUES ('$fromUser', '$today', '$message', '$exiHash')");
}
?>
</form>
I dont know what to do anymore. It used var_dump($exiHash); which does show me the hash number I want to store but it only stores a 0.
Change your action attr in form tag.
it must me like
action="exiConversations.php?<?=$_GET['hash']?>"
but it will work only if you have current URL like ....?hash=45345345
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
Sorry I am asking a question that has been asked before but in spite of reading all of them, I am still confused what to do.
What exactly I have to do to implement the remember me feature in my website I am building as my final year project. Is calling the function "setcookie()" alone sufficient?
setCookie() is all you need.
You can use it like this:
$cookie_value = 'MyUsername';
$cookie_expire = time() + 60*60*24*365;// 365 days
$cookie_path = '/';
setcookie('remember_me',$cookie_value, $cookie_expire, $cookie_path);
On the next pageload, you can fetch the remember_me cookie value with:
$_COOKIE['remember_me'];
But the 'next pageload' part is important because PHP cookies cannot be set and also read in the same browser action.
In the most simple way possible. The way I would bring this all together for demonstration for your project is have a php page with an html <form> that posts to itself.
Your filename would be something like my_form.php
inside it would be:
<?php
// If we received a username from the form, remember it for a year.
if( $_POST['username'] ):
setcookie('remember_me',$_POST['username'], time()+60*60*24*365, '/');
endif;
?>
<?php
// Display a message if the user is remembered.
if( isset($_COOKIE['remember_me']) ):
echo '<h2>Welcome back, '.$_COOKIE['remember_me'].'!</h2>';
endif;
?>
<form action="" method="post">
<input name="username" type="text" placeholder="Your Username" value="<?php echo $_COOKIE['remember_me'] ?>" required />
<button type="submit">Remember me</button>
</form>
This form submits to itself. If you see the username you just entered in the welcome message, then it is remembering you.
Very important! The setcookie() call in PHP must be the first thing in your my_form.php file. Otherwise setcookie() will not work if any output has happened to the web-browser before you call the setcookie() function.
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;
This question already has answers here:
HTML Form to post on php page
(3 answers)
Closed 9 years ago.
Sorry if this has been asked before.
But what is the simplest method to post some text to a site and then display it like on a blog. I don't want it to be a blog, just a textarea, where you can type some text and submit. Like so:
<h1>Post</h1>
<form action="post.php" method="POST">
<textarea name="texty" id="texta" cols="30" rows="10"></textarea>
<input type="submit" value="Post!"/>
</form>
Can someone help me with the PHP to transfer that information and post it on the site so that it stays there like a blog. Any help is appreciated.
just :
<?php
if(isset($_POST['texty'])){
echo htmlentities($_POST['texty'])
}
?>
And look Mysql ;)
INSERT INTO...
You can access POST data via $_POST[] see http://php.net/manual/en/reserved.variables.post.php.
To store it permanently you need to use a data store, one would be a database such as MySQL. That's another chapter though and is not in the scope of this question.
i think u can send the form by ajax : $('form').ajaxForm({url: 'respond.php', success:function(data){ $('.blog').append(data)}}) and in respond.php u should insert the input into database like => if(isset($_POST['submit']) and !empty($_POST['texty'])){ $input = mysql_real_escape_string($_POST['text']); $input = htmlentities($input); now u can insert $insert = mysql_query("insert into tablename(input) values('{$input}') "); if($insert){ echo $input ; } i am new in php. so i think this works
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 1 year ago.
Improve this question
I am developing a site in PHP that allows users to sign up and enter in some information, and I would like to give each user a unique URL. When the user logs in to his profile, I want JUST the username to be passed to the URL (like www.mysite.com/profile.php?user=username) in order to rewrite it later. However, I'm using the $_POST method, and I'm concerned if I use $_GET while logging in the password will be passed to the URL as well. What should I do?
There shouldn't really be a problem doing this. You could simply use a POST method that points to a URL with a GET parameter.
So you make a POST request to:
www.mysite.com/profile.php?user={$username}
This way the user variable in the URL doesn't need to be used in the authentication.
Consider this for a simplistic example:
<form method="post" action="/profile.php?username=hasan">
<input type="text" name="username" value="hasan" />
<input type="text" name="password" value="********" />
</form>
The URL you are posting to doesn't have to be hard coded either - you could always dynamically add the user name before submitting the form.
On the link to or redirection you can add
Link to the profile
and after you read it (in php file) with $_GET['user']
Since the authentication presumably happens in a separate script, e.g. login.php, it's just a matter of appending the username to the profile URL when redirecting upon successful login.
header('Location: profile.php?username='.$username);
Lix's answer is best and is what you should be doing, but assuming you don't have a login.php and for some strange reason want to go straight to profile.php?user to login, you can use this javascript:
<script type="text/javascript">
$(document).ready(function() {
$("#theForm").submit(function() {
$(this).attr("action", $(this).attr("action") + $("#usernameInput").val());
return true;
});
});
</script>
And your form looks something like:
<form action="http://www.mysite.com/profile.php?user=" method="post" id="theForm">
<input type="text" name="usernameInput" id="usernameInput">
<input type="password" name="passwordInput" id="passwordInput">
<input type="submit" name="submit" value="Login">
</form>
And then your action will change to the inputted username upon submit. But still, you should have an intermediary login.php that redirects to profile.php?user.