displaying html form inputs that gets submitted to the browser? - php

I have a basic html form with textarea input and a submit button. I want to make it so that everything in the textarea gets submitted to the database and retrieved to be posted on the homepage of my website. An example would be the structure of stackoverflow, after posting this question it gets displayed on the homepage alongside other questions submitted in the past. I am using php and mysql. Please give me some direction and hopefully examples to how I can go about this. Thanks

<form action='index.php' method='post'>
<textarea name='data' rows='2' cols='20'></textarea>
<input type='submit' name='submit' value='store'/>
</form>
To Store In database:
<?php
if(isset($_POST['submit']))
{
$con = mysql_connect('localhost','root','');
mysql_select_db('database_name',$con);
$data = $_POST['data'];
mysql_query("insert into table_name values ('$data')");
mysql_close($con)
{
?>
To Display in Home Page :
$con = mysql_connect('localhost','root','');
mysql_select_db('database_name',$con);
$q = mysql_query("select * from table_name");
while($r = mysql_fetch_array($q))
{
echo $r['data'];
}
Although this is the simplest and most oldest method but yet it can help you.

On The Form where textarea and submit button is present, just on click of submit, add the action where you can insert a query for inserting a record into the database.
And for your home page where you have to show the post . just add select query for posts And display all the posts according to your requirement.

Related

Get submit button value from multiple buttons php

I have a dynamic menu inside a form element code is here
<form method="GET" action="index.php">
<?php
display_menu(); // this function generates menu items
?>
</form>
after the menu is generated every menu item is a submit button of the above form I want to get input of a single element by name or id attribute of submit button, and load a post from database.
<form method="GET" action="index.php">
<input type="submit" name="page-1" id="page-1" value="page-1">
<input type="submit" name="page-2" id="page-2" value="page-2">
<input type="submit" name="page-3" id="page-3" value="page-3">
<input type="submit" name="page-4" id="page-4" value="page-4">
</form>
so when any input button is pressed the function display_post() is called. Code of the function is as follows:-
function display_post(){
$conn = mysqli_connect('localhost', 'root', '', 'posts') or die('cannot connect');
if($_SERVER['REQUEST_METHOD'] == 'GET' ){
$blog_post_id = $_GET["id"];
$sql = "SELECT * FROM blog_posts where id='blog_post_id' ";
$result = mysqli_query($conn, $sql) or die('cannot load');
while ($row = mysqli_fetch_assoc($result)){
if($row > 0){
echo '<div>'.$row['content'].'</div>';
}else echo 'no posts';
}
}
}
However, the display_post() method is called inside a content tag whereas the display_menu() is called inside another div.
So the problem is I'm unable to get the id of the to submit button any help will be appreciated thanks in advance.
When you click on the submit button you will get all inputs values of the form.
Do one thing put this statement print_r($_GET);exit; in display_post() as a first line and see what you get after clicking on submit button.
Note: 1. you want to get the value of clicked button then you should use javascript or jQuery and Ajax.
2. You can not get input fields value by fields ID.
ex. if we have field <input type="submit" name="page-1" id="page-1" value="page-1">
then we can get its value as:
echo $_GET['page-1'];
page-1 is the input field name.
You can make 5 different forms for each button and change the action to
action="index.php?id=1"
Then use $_GET['id']
Also change id='blog_post_id' " to id='$blog_post_id' "
If I am guessing right what is your difficulty then
$blog_post_id = $_GET['id'];
$sql = "SELECT * FROM blog_posts where id='".$blog_post_id."' ";//I modified this line
$result = mysqli_query($conn, $sql) or die('cannot load');

Writing single page logging tool in php

Im making a small php webpage which I plan to use to track on which subjects a helpdesk receives calls. My database has 3 important fields: id, name, and amount for each subject.
On my page I have a form with a dropdown list where you select a type of call and click submit. The idea is that every time you click submit the page reloads and the amount in the database for the chosen id is heightened by 1.
The form gives me the id and name for each call:
<form method="post" action="index.php">
<select class="select" id="calltype" name="calltype">
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value=".$row["ID"].">".$row["NAAM"]."</option>".PHP_EOL;
}
}
?>
</select></br>
<input class="input" type="submit" name="Submit" value="Submit">
</form>
This part works, if I echo $_POST['calltype'] I get the correct ID. What I can't get to work is the update statement which I want to heighten the counter, like:
if(isset($_POST['calltype']{
mysqli_query("UPDATE calls SET amount=(amount+1), WHERE id = $_POST['calltype']");
}
How would I go about this? I tried several methods but can't get it to work
besides for the extra comma, interpolation with the POST array like this is risky. maybe try:
mysqli_query("UPDATE calls SET amount=(amount+1) WHERE id = " . mysqli_real_escape_string($link, $_POST['calltype']) . " ;");

MySQL Jquery update array?

i have a small website where people can submit name ideas for a project i am working on
Two things i want to implement are an upvote button (+1 to current score)
and when someone submits a name it will update the mysql array using ajax.
I am very new to Ajax and i have managed to work some code together that submits a name without leaving the current page.
This is my code for the mysql array that displays underneath the name submission form.
How can i link this upto a form submit button for refresh when a name is submitted?
<?php
$result = mysql_query("SELECT * FROM namevotes ORDER BY upvote desc")
or die(mysql_error());
echo "<center>";
echo "<table border='1' id='tablea'>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['name'];
echo "<tr><td>";
echo $row['upvote'];
echo "</td></tr>";
}
echo "</table>";
echo "</center>";
?>
I am want to add a button next to each row that will increase the "upvote" by one, i know how to rig one up that will manually upvote a single entry, im not sure how to direct it to each array entry.
Method is very simple:->
1> add button in while clause using <button id="{row['id']}">like me</button>
2> create a jquery element in webpage that onclick on hitlike button trigger a event: fetch data from url by get {or post} method with data ?hitlike=id
3> before code in php tag, put if statement that if(isset($_GET['id'])) then UPDATE table SET likes=likes+1 WHERE id=$_GET['id']
4> rest code will be the same to show data
//I haven't tried this but I think it should work

Passing value from a dropdown list from one php to another

I have
A dropdown list populated from a MySQL table.
A button.
Another php page
What I need to do:
On clicking the button, pass the value selected from the dropdown list as a variable to second php page.
Pass that variable into a mysql query on the second php page. eg: $result = mysql_query("Select * from table where name like "$drop_down_value");
I am new to php and pardon my naivety.
This is my code to get values for the dropdown list:
function dropdown_query()
{
mysql_connect("localhost","root","root") or die("Failed to connect with database!!!!");
mysql_select_db("student_test") or die("cannot select DB");
$result = mysql_query("Select * from marks");
if($result=== FALSE){
die(mysql_error());
}
while($r=mysql_fetch_array($result))
{
echo '<option value="' .$r['marks'] . '">' . $r['marks'] . '</option>';
}
and this is my HTML part:
select name="dropdown" onchange="somefunc()">
<option value="">Select...</option>
<?php dropdown_query()?>
</select>
Lets say I use a similar query on another php. But would use the value selected on this page as a variable in that query.
By wrapping the drop down in a form with POST method, you can send the value to the next page and retrieve via $_POST['your_field_name']. See the docs.
Your page1 will have a form something like
<form action="page2.php" method="post">
<p>Name: <input type="text" name="name" /></p>
<p><input type="submit" /></p>
</form>
And in page2.php you can do something along the lines of
$name = $_POST['name'];
$sql = "SELECT * FROM table WHERE name LIKE '$name'";
...
(But make sure to scrub the user input before using it on page2.php!)
you will need javascript to do this. The page already finish loading. php script wont work after page finish loading. try jquery, ez to use
The answer to your question is to use the $_POST or $_GET superglobal variables in PHP. You can use either of these to obtain the value of the dropdown list from the first page after the button is clicked. For example, you could use the following code to obtain the value of the dropdown list:
$drop_down_value = $_POST['dropdown'];
Then, you can pass this variable into your MySQL query on the second page, as you have outlined in your question:
$result = mysql_query("Select * from table where name like "$drop_down_value");

How to store the result of a radio button with a database value, back into the database?

Been stuck on this bit of code for a while now. What I'm doing in 'do_assignments.php' (below) is creating a loop that outputs each question in my database and every possible answer associated with that question.
<form method="post" action="user_storeresults.php"> $query = "SELECT * FROM questions, question_choices WHERE q_topic = 'PhoneGap' AND visible = 1 AND q_type = 'Multiple' AND questions.q_id = question_choices.question_id";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
if($row['q_id'] != #$qid){
echo "<h4><strong>" . $row['q_string'] . "</strong></h4>";
#$qid = $row['q_id'];
}
echo "<input name='".$row['choice_id']."' value='".$row['correct_choice']."' type='radio' />";
echo $row['choice_string'] . "<br/>";
}
?>
<p><input type="submit" name="submit" value="Submit"><p>
</form>
<?php
The value of each radio button contains data from a field in 'correct_choice' in my database, where there is wrong answers (0) and a correct answer (1) associated with each question.
All of this works well, but because i'm trying to submit a post from a radio button that doesn't have a specific name or value (only what it retrieves from the database), i can't quite figure out how i would be able to retrieve these values in the next page 'user_storeresults.php'...
Any help would be much appreciated. If i'm being a bit too vague then say, and ill try and clear it up as much as possible.

Categories