PHP - Simplest Way to Post & Display [duplicate] - php

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

Related

PHP and html post button value

I have a html page where the user can input some text, it is then posted to a php file and then stored in a database.
<form action="postphp.php" method="post" enctype="multipart/form-data">
<center><input id="postTitleStyle" type="text" name="title" size="100" maxlength = "180" ></center>
<center><textarea id="postTextStyle" name="Text" rows="10"cols="96" maxlength = "40000"><?php echo $text;?></textarea></center>
<center><input id="postTagStyle" type="text" name="tags" size="100" maxlength = "900" ></center>
<center><input type="submit" class = "Post2" value="post"></center>
</form>
Above is my current code for posting the data in the text field to a php file. I want to be able to click a button that when clicked will not go to the php file it will be stored and then when the user clicks the submit button it is posted. For example the user clicks it, a one is stored and then sent later when the user clicks the submit button after they are finished filling in other details. Is this possible?
P.S. I want to avoid Javascipt as much as possible for the moment, so if there is a non-java way of doing it then it would be much appreciated.
Many thanks, Jack.
There are two easy solutions to this problem without using Javascript. I'm assuming by your wording that you can currently post a form, but you don't know how to do so without leaving the current page. That's what I'll be answering below, but please note that there is no way to post a form without reloading at all without Javascript.
Solution 1: Put the PHP code into the same page the form is on and change the form tag to: <form action="" method="post" enctype="multipart/form-data">
A blank action field will cause it to run the PHP on the current page. You will likely need to look into using isset($_POST['submit']) in PHP, which will check whether the submit button has been clicked on before running that particular PHP code. You can read more about that HERE.
Solution 2:
In the postphp.php file that's currently linked to in your action field of your form, you could use a PHP header that will redirect the user after the PHP code is ran.
E.g.
<?php
{All your form processing code goes here}
header('location: my_form_page.php');
?>
In this example, my_form_page.php is the page on which your form is on. You can read more about headers in the answer of THIS question.
Hopefully this helps a bit.
$title = $_POST['title'];
$text= $_POST['text'];
$tags = $_POST['tags'];
mysql_query("INSERT INTO `table_name` (`colname1`,`colname2`,`colname3`) VALUES ('$title,'$text','$tags')");
$id = mysql_insert_id();
if($id){
echo "inserted";
}else{
echo "Not inserted";
}
For this you need to use Ajax (JavaScript will be used) because you need a button which send data to server without form submission and page reload it can be easily achieved using Ajax.

Create a page with user credentials from PHP form [closed]

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

How to make a BitCoin API call using PHP? [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
I am making a website that gets information about a bitcoin wallet balance. What I am trying to achieve is for whatever the persons text input is to be sent to the link but sent like "http://link.com/ + input"
I am new to PHP so I am not sure what to do next or what I have done wrong so far.
<?php
echo ' <form action="https://blockchain.info/q/addressbalance/" method="post">
BTC Address: <input type="text" name="address"><br>
<input type="submit" value="Submit">
</form>'
$input = $_POST['post'];
echo $input
?>
Old Answer:
Use string concatenation.
For example:
$link = "http://link.com" . $input;
Update:
It seems like what you actually want to do is to redirect the user based on their input. For this, you should use the header() function.
Here is an example as to how you can use the function for redirection purposes.
header('Location: ' . $link);
die();
Note that this must go at the very top of the page, before any HTML is outputted. (So of course, your code for $link should go before it as well)
As for your <form action="https://blockchain.info/q/addressbalance/" method="post">, you will want to change the action to the current document. This is because $_POST['post'] only receives data after you pressed submit. Changing it to action="" generally defaults to this behavior.

put the value of textbox into a variable using PHP (not post method) [duplicate]

This question already has answers here:
Among $_REQUEST, $_GET and $_POST which one is the fastest?
(15 answers)
Closed 8 years ago.
i wanna put the text that user entered in textbox in a variable
i don't want to send it to another page.
how can i do it!
in asp.net it would be :
string a = textbox1.text;
how could i do it using php?
html :
<html>
<body>
<input type="text" id="news">
</body>
</html>
php variable :
$news_text;
i wanna put text that user entered into $news_text
please help me!
ASP.NET uses forms. There is no way to avoid using POST/GET/etc.
<?php
$news_text = isset($_REQUEST['news']) ? $_REQUEST['news'] : '';
?>
<html>
<body>
<form method="get">
<input type="text" id="news"/>
</form>
</body>
</html>
You have to send it to at least that same page to get a post value.
$news_text = isset($_POST['news_text']) ? $_POST['news_text'] : '';

PHP: Website getting hacked weirdly [duplicate]

This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 9 years ago.
I'm currently developing a web interface in PHP/HTML for a Database course project.
Basically, there is an input field :
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
that allows one to search for things in my DB.
Yesterday evening, after uploading my new index.php, I refreshed the page and there was (was I though it was) some sort of Injection because my page was entirely filled with spam ("YO MAMAYO MAMAYO MAMA etc,").
I secured the form using the "htmlspecialchars()" php function. And once again, I just uploaded the new index.php just 10 mins ago and the page was filled with "YO MAMA" right after I refreshed.
Has anyone an idea about that ? And how can I check/secure my page ?
Thanks
EDIT : The code of the form is the following :
<div id="searchbox">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Query database : <input type="text" id="field" name="query">
<input type="submit" name="submit" value="Search!">
</form>
</div>
and I just secured with :
if(isset($_POST['query']) && !empty($_POST['query'])) {
$param = htmlspecialchars($_POST['query'], ENT_QUOTES);
...
The inputs I can give are anything, the goal is to search for people or events or etc.
I only have a database class file which I include in my index.php
EDIT2 :
Sql query is the following :
SELECT p.idParticipant As id, a.name AS name, c.countryName AS country,
count(g.idGame) AS countGames
FROM Athlete a, Country c, Game g, Participant p, Event e
WHERE a.idAthlete = p.fkAthlete
AND p.fkCountry = c.idCountry
AND p.fkGame = g.idGame
AND g.idGame = e.fkGame
AND a.name LIKE '%$param%'
GROUP BY a.name
ORDER BY a.name;
In addition to use real_escape_string function (with mysqli or PDO), I would change FTP password AND database user and password.
Whitelist you input let say if only string is allowed use is_string() to verify it.
Use PDO and parameterized queries. Stop creating queries by concatenating input.
And stop using the mysql_* set of functions. Right now. Every time you type it in a php source file, $deity kills a kitten. Stop this slaughter please.

Categories