I have following code on the top of product detail page
<?php
include("include/connection.php");
session_start();
$ses_id = session_id();
$ID = (int)$_REQUEST['id'];
if (!isset ($_POST['submit']))
{
?>
<div class="row">
<div class="span9">
<div class="span6 ">
<h4>Review(0)</h4>
<p>There are no review for this product</p>
<h4>Write a Review</h4>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>Your Name:</p>
<input type="text" id="txtname" name="Rname" placeholder="write your name..." />
<p>Your Review</p>
Excelent<input type="radio" name="reviewbtn" class="radio" value="Excelent" />
Good<input type="radio" class="radio" name="reviewbtn" value="Good" />
Poor<input type="radio" class="radio" name="reviewbtn" value="Poor" /><br/>
<textarea id="txtreview" name="txtbx" cols="50" rows="10"
class="container-fluid"></textarea>
<input type="submit" value="submit" class="btn" />
</form>
</div>
</div>
</div>
<?php
} else {
$Rname = mysql_real_escape_string(stripslashes(htmlentities($_POST["Rname"])));
$Reviewbtn = $_POST["reviewbtn"];
$Txtbox= mysql_real_escape_string(stripslashes(htmlentities($_POST["txtbx"])));
$sql=mysql_query ("INSERT INTO reviews (Name, Comments,Rating) VALUES('$Rname','$Reviewbtn','$Txtbox')", $con) or die (mysql_error());
echo "Record added succesfully. You will be redirected to previous page in 5 seconds";
header( "refresh:5;url= product_detail.php" );
}
mysql_close($con);
?>
here I am getting error
Notice: Undefined index: id in \product_detail.php on line 5
( which is $ID = (int)$_REQUEST['id']; )
basically I am building a rating form on a product,please if someone can modify the code so form submission could be done using ajax jquery call so page should not load again
remove int from it there no need of it.
$ID = $_REQUEST['id'];
There is no Element named ID in your form
So when you are going to catch this it occurred error
$ID = $_REQUEST['id'];
^ provide actual form element's name here
When you coming to this page from previous page it is not showing error
after submitting from it is showing error.
If it is, add an hidden element into your form
<input type='hidden' name='id' value="<?php echo $ID; ?>">
It will solve the problem
Use some other word as name attribute rather than naming it as id you can set it to user_id or something else you want,because it is not a good practice
if(isset($_REQUEST['user_id'])){
$ID = $_REQUEST['user_id'];
}
Related
I created a section for editing. When I edit the information and click the save button, the information is not saved and the header section does not display completely.
<?php
if (isset($_POST['submit_btn']))
{
$id = $_POST['id'];
$fn = trim($_POST['name']);
$ln = trim($_POST['lastname']);
$age = trim($_POST['age']);
$q = "UPDATE `users` SET `fn` = '$fn',
`ln` = '$ln',
`age` = '$age'
WHERE id = '$id'";
mysqli_query($dbconnect,$q);
if (mysqli_affected_rows($dbconnect) > 0)
redirect("?msg=ok&id=**$id**");
else
redirect("?msg=error&id=**$id**");
}
else
echo ("Not In If(isset)");
?>
<form action="" method="post">
<label for="name">FirstName:</label>
<input type="text" name="name" id="name" value="<?php echo $row['fn']?>">
<br>
<label for="lastname">LastName:</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $row['ln']?>">
<br>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $row['age']?>">
<br>
<input type="submit" name="submit_btn" value="Save">
<a href="index2.php">
Back
</a>
</form>
</body>
Bold sections do not work here.
Below is a picture of the result:
In the link that I specified, after clicking on save the ID will not be displayed and all the information filled in the forms will be lost.
Sorry if the result is styleless and boring and I just created this page to practice php😁
Thank you for being responsive🙏🙏🙏
You are mistaking a POST request with a GET request.
Part, which appears in the URL is sent to the webserver in GET request.
Your form is submitting POST request to the webserver, logic in the code does the same, but you are trying to display information from url (GET).
Please check the examples in php.net:
POST variables: https://www.php.net/manual/en/reserved.variables.post.php
GET variables: https://www.php.net/manual/en/reserved.variables.get.php
You can take an example with GET request variable below, however, be careful with trusting the "end client" and always prepare your statements, which you send to your database to execute queries.
if (isset($_GET['submit']))
{
$number = $_GET['number'];
echo $number
? "Number which was submitted: $number <br>"
: 'Number is not set';
} else {
echo 'Form has not been yet submitted';
}
?>
<form action="" method="get">
<input type="number" name="number" placeholder="Number">
<input type="submit" name="submit" value="Save">
</form>
I'm trying to update a category name with no luck...
on the functions.php i have a function to show all categories:
function get_all_categories(){
global $connection;
$query = 'SELECT * FROM categories';
$select_categories = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($select_categories)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<tr>";
echo "<td>$cat_id</td>";
echo "<td>$cat_title</td>";
?>
<form method="post">
<input type="hidden" name="delete_cat_id" value="<?php echo $cat_id; ?>">
<?php
echo '<td class="text-center"><input class="btn btn-danger" type="submit" value="Delete" name="delete"></td>';
?>
</form>
<form method="post">
<input type="hidden" name="update_category_id" value="<?php echo $cat_id; ?>">
<?php
echo '<td class="text-center"><input class="btn btn-info" type="submit" value="Update" name="update_category"></td>';
?>
</form>
<?php
// echo "<td class='text-center'><a class='btn btn-info' href='categories.php?update=$cat_id'>Update</a></td>";
echo "</tr>";
}
}
the function is shown in the file categories.php:
<form action="" method="post">
<div class="form-group">
<label for="my_new_cat">Add a new category
<input class="form-control" type="text" name="my_new_cat">
</label>
</div>
<input type="hidden" name="category_id" value="<?php echo $cat_id; ?>">
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Add Category">
</div>
</form>
<?php
if (isset($_POST['update_category'])) {
include "includes/update_categories.php";
}
?>
my code in the update_categories.php is:
<?php
$selected_cat_id = $_POST['update_category_id'];
$query = "SELECT * FROM categories WHERE cat_id = $selected_cat_id";
$select_edit_categories = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($select_edit_categories)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
}
?>
<form method="post">
<div class="form-group">
<label for="updated_cat_name">Update category</label>
<input class="form-control" type="text" name="updated_cat_name" value="<?php echo $cat_title; ?>">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="update_cat_title" value="Update Category">
</div>
</form>
<?php
if(isset($_POST['update_cat_title'])){
$updated_cat_title = $_POST['updated_cat_name'];
$edited_category_id = $_POST['category_id'];
$update_cat_query = "UPDATE categories SET cat_title = '$updated_cat_title' WHERE cat_id = $edited_category_id ";
$update_query = mysqli_query($connection,$update_cat_query);
checkQuery($update_query);
}
?>
there is a button to update the category, the category name is getting into the input field in the code above.
when I click the UPDATE button a new field is revealed via include "includes/update_categories.php"; containing the category name inside it.
when I change the name in the input field and click "Update Category" under the new field of category name I get nothing and nothing changes in the DB.
also, no errors presented...
any ideas?
thank you!
$_POST['update_category_id'] is accessed first thing in your code but there is no input for update_category_id.
Did you missed to set a hidden input for update_category_id in the form?
You may edit your code to add hidden input around
<input class="form-control" type="text" name="updated_cat_name" ...
<input type="hidden" name="update_category_id"
value=<?php echo $selected_cat_id; ?> />
Edit :
form containing update_category_id in "functions.php" should have action="categories.php".
where is update_category set in "categories.php" that is being checked in if (isset($_POST['update_category'])) ?
Can you confirm if the file "includes/update_categories.php" is being included ?
The hidden input is still missing in form in file "includes/update_categories.php"
<input type="hidden" name="update_category_id" ... in "functions.php" just passed to "categories.php" to be able to display information of selected "update_category_id", where as <input type="hidden" name="update_category_id" ... is required save the changes made in form in file "includes/update_categories.php".
OK
After a long searching for the answer including the wonderful people here trying to help, I have figured it out.
The problem was in this code:
if (isset($_POST['update_category'])) {
include "includes/update_categories.php";
}
The problem was that when i clicked the "Update" button created in the code above, it included the query for getting the value from the SQL field AND the query to update the value in the SQL.
The thing is when i click the "Update" button again in the code above, the POST is changing to if(isset($_POST['update_cat_title'])) instead.
The result is that the if (isset($_POST['update_category'])) POST in no longer avaliable, which causes the include of the form to be removed.
Therefore, the UPDATE query is no longer in this page and the results are not sending.
So i put this:
if(isset($_POST['update_cat_title'])){
$newCatName = $_POST['updated_cat_name'];
$catId = $_POST['category_id'];
update_category($newCatName, $catId);
}
(could have put all the function in the same file but decided to move it to the "functions.php" file instead).
So i created this function:
function update_category($newCatName, $catId){
global $connection;
$updated_cat_title = escape($newCatName);
$edited_category_id = escape($catId);
$update_cat_query = "UPDATE categories SET cat_title = '$updated_cat_title' WHERE cat_id = $edited_category_id ";
$update_query = mysqli_query($connection,$update_cat_query);
checkQuery($update_query);
header("Location: categories.php");
}
Now everything works great,
Thanks All!
I'm trying to store a Boolean value in my database based off a checkbox(Watched) in a form. If the checkbox isn't checked on form submit, it should send a 0; Otherwise a 1.
However, it sends a value of 0 no matter if it's checked or not. The Title Field and Genre field send perfectly, however the Watched conditional is not working correctly. I'm stumped and having a hard time wrapping my head around this, any thoughts?
<?php
require 'config/config.php';
require 'config/db.php';
// Check for Submit
if(isset($_POST['submit'])) {
// Get Form Data
$title = mysqli_real_escape_string($conn, $_POST['title']);
$genre = mysqli_real_escape_string($conn, $_POST['genre']);
if (isset($_POST['watched']) && ($_POST['watched'] == "value")) {
$watched = 1;
} else {
$watched = 0;
}
$query = "INSERT INTO movies(name, genre, watched) VALUES('$title', '$genre', '$watched')";
if (mysqli_query($conn, $query)) {
header('Location: '.ROOT_URL.'');
} else {
echo "ERROR: " . mysqli_error($conn);
}
}
?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="md-form">
<input type="text" id="form1" class="form-control" name="title">
<label for="form1" class="">Title</label>
</div>
<div class="md-form">
<input type="text" id="form1" class="form-control" name="genre">
<label for="form1" class="">Genre</label>
</div>
<div class="form-group">
<input type="checkbox" id="checkbox1" name="watched">
<label for="checkbox1">Have you watched it already?</label>
</div>
<input type="submit" name="submit" value="submit" class="btn btn-primary">
</form>
You never set an actual value for the checkbox in your form HTML, so the value of the checkbox will never exist which means your if statement will always return false.
Simply change this:
<input type="checkbox" id="checkbox1" name="watched">
to this:
<input type="checkbox" id="checkbox1" name="watched" value="value">
I want to edit mysql data using a contact form. When the user clicks edit on the specific post they want to change, it takes them to a form that has the post values in the input fields, and they can change whatever they want about the post. I already have this part working but now I need the change to actually take effect once the user clicks submit. How can I accomplish this?
Here is the PHP that displays all the text from the blog post in mysql into the input fields when the user clicks edit.
<?php
include "../php/db_connect.php";
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM articles WHERE id='$id'");
while($row = mysql_fetch_array($sql)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$written_by = $row['written_by'];
$category = $row['category'];
}
?>
<div class="content_wrapper">
<h1 class="content_heading">Edit A Blog Post</h1>
<form method="post" action="php/edit_article_process.php" enctype="multipart/form-data" id="upload_article_form">
<span>Title</span>
<input type="text" name="title" class="input" value="<?php echo $title; ?>"/>
<span>Article</span>
<textarea rows="20" name="ckeditor1" class="input" style="resize:none;"><?php echo $content ?></textarea>
<span>Author</span>
<input type="text" name="written_by" class="input short_input" value="<?php echo $written_by; ?>"/>
<span>Category - <i>Spell it right nigga</i></li></span>
<input type="text" name="category" class="input short_input" value="<?php echo $category?>"/>
<input type="submit" name="update" class="input submit_button"/>
</form>
<?php } ?>
</div>
Now all I need is for the change to take effect when the user clicks submit.
Your form attribute method has a value of post which is not the same method you used inside the isset() function
You should use:
$_GET['id'] //if your method="get"
and
$_POST['id'] //if your method="post"
if you want the change to appear directly you should re-fresh the page after editing the values
header('Location: www.samepage.php');
I have a functioning "Account Notes" page that allows the user to add notes to their account for their own uses. I want to allow my admins to input notes into user accounts without logging in. I have a Dropdown on the Admin page that allows the admins to select a user. After selecting the user I want to echo the notes already in their notes section into the TextArea below and allow an update to the notes.
function getNotes(){
global $id;
$result = query("SELECT * FROM `extras` WHERE `userid`=".$id);
while ($row = mysql_fetch_assoc($result)){
return $row['notes'];
}
}
<form action="update.php" method="post">
<h4 class="widgettitle title-primary">Account Notes</h4>
<br/>
<textarea rows="20" scroll="auto" class="txt" name="notes" style="width: 80%;"/><?=getNotes();?></textarea>
<div style="text-align:center;"><button class="btn btn-primary">Update</button></div>
<input type="hidden" name="formtype" value="updatenotes" />
<input type="hidden" name="redir" value="notes.php" />
</form>
</form>
</div>
</div><!--row-fluid-->
All of the above code works correctly, so there are no issues above. My issue lies below...
The following code is what I have on my admin.php page for the user notes addition code...
<div id="addusernotes">
<form action="update.php" method="post">
<h4 class="widgettitle title-primary"><center>Add User Notes</center></h4><br/>
<?php
echo '<select id="user" name="user">';
$user = query("SELECT * FROM `users` ORDER by `user`");
if($user && mysql_num_rows($user))
{
while ($row = mysql_fetch_assoc($user))
{
echo '<option value="'.$row['id'].'">'.$row['user'].'</option>';
}
}
echo '</select>';
?><br/>
Notes:<br/>
<textarea rows="5" scroll="auto" class="txt" name="adminusernotes" style="width: 80%;"/></textarea><br/>
<input type="submit" name="adminnotes" value="Add Notes"><br/><br/>
<input type="hidden" name="formtype" value="adminusernotes" />
<input type="hidden" name="redir" value="admin.php" />
</form>
What JQuery do I need to add to the admin.php file to allow the Notes from the DB to display in the TextArea? I have been stumped on this for nearly 3 days.....Any help will be more than appreciated!!!
FYI....I have intentionally left out some of my "proprietary code for security reasons...
To alter a textarea's content with jQuery, you simply use:
$("#element-id-here").val("whatever value...");
But give your textarea an ID first. And you can change the value to a variable too.
If you want PHP to echo the textarea contents:
<textarea><?php echo "contents here"; ?></textarea>