+ 7
User Input in PHP
I just recently finished the PHP course, and it seems that the only way to get user input is by using HTML. Does anyone know if there's a way to get user input directly from within PHP, such as input() in Python or cin in c++? Thanks in advance!
5 Réponses
+ 13
You don't.
PHP is used to scrape stuff out of HTML.
So you create a HTML form.
Then in the form element you can have:
method="POST"/"GET"
as an attribute.
Then,
<?php
a=$_POST["value"]/$_GET["value"];
echo a;
?>
+ 8
No way, it's restricted with HTML form tag.
+ 5
No, there isn't a way it's only working with html:(
+ 5
@Ahmad Nope, you can try ;)
+ 2
There is no other way. You have to use html to pass data (input). Never heard of php prompt or something like that.
But the php part is not so bad after all... (joke, bad one i know)
So the basic flow looks like this :
So html form looks like that:
<form action="send_to_database.php" method="post">
<input type="text" name="some_text">
<input type="submit">
</form>
Then the fun with PHP starts. PHP script in steps
1. check connection:
$con = mysql_connect("server address","DB_id","DB_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
-------------------------------
2. Select needed database:
mysql_select_db("DB_id", $con);
-------------------------------
3. Now start transfering values:
$sql="INSERT INTO table_name (some_text)
VALUES
$_POST[some_text];
-------------------------------
4. Again now we check if everything is OK. If not we want to know.
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "success!";
-------------------------------
5. Close connection:
mysql_close($con)
Now the closing connection part is essential for safety reasons and to avoid problem like you want to send some data from another form and connection to the former table is still open...
Imagine now that address data tries to fit in personal data table - Impossible because column names are different so php will throw error after error.
Nothing bad in testing phase but when site is online...
And that should be it. I hope i`ve managed to clairfy this a bit.