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');
Related
I have a database that contains an autoincrement id, the location and the date's trip. With this code I can show on display the result of my query.
$mysqli = new mysqli("localhost", "root", "password", "trip");
$result = $mysqli->query("SELECT * FROM news WHERE location = '$location'");
echo "<br/><h3>"."Result, I found " .$result->num_rows. " results.". "</h3><br/>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["location"]."</td><td>".$row["date"]."</td><td>".' <button type="submit" value="reserve"/>'. "</td></tr>";
}
How can I know which button the user click to reserve his trip?
$('button').on('click',function(){
alert($(this).val()); // do anything what you want
});
Sample snippet:-
$('button').on('click',function(){
alert($(this).val()); // do anything what you want
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>pahse 7, mohali</td>
<td>04/06/2019</td>
<td><button value="Click Me!">Click Me</button></td>
</tr>
Note:- instead of submit button use input type="button", as submit button used to submit form normally.
You don't need jQuery, or JavaScript at all, unless you want to submit the form with AJAX, which you haven't mentioned. You just need to fix your HTML.
<button> is not an empty element; it needs a closing tag. The text on the button goes between the tags. Give the button a name and assign the row id as its value.
$button = "<button type='submit' name='id' value='$row[id]'>Reserve</button>";
Then you can get the id of the clicked button from $_POST['id'] in the PHP script that handles the form submit.
Also, with this code
$result = $mysqli->query("SELECT * FROM news WHERE location = '$location'");
there is no way SQL injection is not a problem, regardless of what you've done on the client side. Client side validation is trivial to bypass. You need to use a prepared statement.
To get particular row information use data attribute in your button and then get that information
$mysqli = new mysqli("localhost", "root", "password", "trip");
$result = $mysqli->query("SELECT * FROM news WHERE location = '$location'");
echo "<br/><h3>"."Result, I found " .$result->num_rows. " results.". "</h3><br/>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["location"]."</td><td>".$row["date"]."</td><td><button type='button' data-bind='".$row["location"]." - ".$row["date"]."' value='reserve'/></td></tr>";
}
Use Jquery to get that information
$('button').on('click',function(){
var info=$(this).attr('data-bind');
alert(info);
});
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.
Via post I send values to a php file that contains the following code:
// Prepare values for database
$username = $_SESSION['username'];
$event_title = $db -> real_escape_string($_POST['create_title_hidden']);
$event_type = $db -> real_escape_string($_POST['create_type_hidden']);
$event_town = $db -> real_escape_string($_POST['create_town_hidden']);
// 1. Identify users_id
$results = $db -> query("SELECT * FROM users WHERE username='$username'");
while ($result = $results->fetch_assoc()) {
$users_id = $result['id'];
}
// 2. Identify towns_id
$query = "SELECT * FROM towns WHERE town='$event_town'";
// Do Search
$results = $db -> query($query);
while ($result = $results -> fetch_assoc()) {
$towns_id = $result['id'];
}
My problem: The second query does not work, i.e. I cannot get the town_id.
I already checked my error log, there is no such error message.
Maybe there is something I forgot to include? Is it possible two have two different queries one after another in a php script?
EDITED:
Here comes my frontend with the form:
<form id="form_create_event" method="post" action="system/upload_event_tourist.php">
<p>
You can now create the event:
</p>
<!-- Forms cannot be nested inside forms, that's why the input fields from first view
need to be inserted into hidden input fields via jquery that will be transferred
via post to upload_event_tourist.php -->
<input type="hidden" id="create_title_hidden" name="create_title_hidden">
<input type="hidden" id="create_type_hidden" name="create_type_hidden">
<input type="hidden" id="create_start_hidden" name="create_start_hidden">
<input type="hidden" id="create_end_hidden" name="create_end_hidden">
<input type="hidden" id="create_picturepath_hidden" name="create_picturepath_hidden">
<input type="hidden" id="create_meetingpoint_hidden" name="create_meetingpoint_hidden">
<input type="hidden" id="create_description_hidden" name="create_description_hidden">
CREATE EVENT
<input type="submit" id="submit_create_event" name="submit_create_event">
</form>
<!-- #form_create_event -->
<script>
// Load form details into hidden input fields and Simulate click on submit
$('.button_create_event').on('click', function() {
// load form details into hidden input fields
var event_title = $('#create_title').val();
var event_type = $('#create_type').val();
var event_town = $('#create_town').val();
var event_start = $('#create_start').val();
var event_end = $('#create_end').val();
var event_picturepath = $('#create_picturepath').val();
var event_meetingpoint = $('#create_meetingpoint').val();
var event_description = $('#create_description').val();
$('#create_title_hidden').val(event_title);
$('#create_type_hidden').val(event_type);
$('#create_town_hidden').val(event_town);
$('#create_start_hidden').val(event_start);
$('#create_end_hidden').val(event_end);
$('#create_picturepath_hidden').val(event_picturepath);
$('#create_meetingpoint_hidden').val(event_meetingpoint);
$('#create_description_hidden').val(event_description);
$('#form_create_event').submit();
});
</script>
Trace out your variables.
$_POST['create_town_hidden']
is assigned by:
$('#create_town_hidden').val(event_town);
which calls the variable event_town. That variable is set here:
var event_town = $('#create_town').val();
which is supposed to select something with an id of 'create_town', but there is no element with that ID in your HTML. Fix that, and your problem is solved.
How do I check with PHP if this code has been clicked so I can use it in an if statement?
<form id="rating" action="index.php" method="post">
Rating
</form>
So if it is clicked I want to use this query:
if () {
$result = mysqli_query($con,"SELECT * FROM users WHERE `approved` = 1 ORDER BY `rating`");
}
You would have to add an input, like <input type="hidden" name="hidden_element" value="data"/> to your form, otherwise there is no POST data for the server to receive.
Then in the index.php script you can check if $_POST['hidden_element'] is set.
For your example:
if (isset($_POST['hidden_element']) {
$result = mysqli_query($con,"SELECT * FROM users WHERE `approved` = 1 ORDER BY `rating`");
}
So since your current form doesn't submit any data i like the technique of if multiple buttons are inside a form each should have its propper name and a type of submit
and on php you check like
if (this button was pressed then)
else if (this button was pressed then)
else (redirect in none or what ever you need to do when landed)
your form, i changed the ahref to an input type submit with the name of button
<form id="rating" action="index.php" method="post">
<input type="submit" name="button"
onclick="document.getElementById('rating').submit();">Rating
</form>
the php action should look like this, you can later implement ajax calling here
if (isset($_POST['button']) === true && empty($_POST['button']) === tre)
{
$result = mysqli_query($con,"SELECT * FROM users WHERE `approved` = 1 ORDER BY `rating`");
}
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");