Rating plugin. Value 0 is inserted into database [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I'm using rateit rating plugin for my website (https://rateit.codeplex.com/).
I'm trying to insert the value that a user selects into my database however every time I do this value 0 is inserted. Here is my code (PHP first):
session_start();
$username = $_SESSION['username'];
if(isset($_SESSION['username'])) {
$username = $_SESSION['username'];
}
else {
header('Location: login.php');
}
$itemid = $_GET['id'];
(HTML):
<div id="itemreview">
<form action="" name="review" id="review" method ="post">
Skin Type: <select name="skintype">
<option selected="">Skin Type</option>
<option value ="Oily">Oily</option>
<option value ="Mixed">Mixed</option>
<option value ="Sensitive">Sensitive</option>
<option value ="Normal">Normal</option>
</select><br>
Rating:
<input type="range" value="0" step="0.5" id="rating">
<div id="stars" class="rateit" onclick="" data-rateit-backingfld="#rating" data-rateit-resetable="false" data-rateit-ispreset="true"
data-rateit-min="0" data-rateit-max="10">
</div>
<br>
<textarea name="review" id="review" cols ="50" rows="8"></textarea>
<input type="submit" name="submitcomment" id="submit" value="Comment!">
</form>
Here's my Jquery/Ajax:
<script>
$(document).ready(function () {
$('#submit').click(function () {
//Create a variable to hold the value entered by the user
var stars = $('#stars').rateit('value');
$.ajax({
url: 'itemreview.php',
type: "POST",
data: { "stars" : stars },
success: function (data) {
alert("Got it!"+ stars);
}
});
});
});
</script>
And here's my php:
<?php
//Insert review into database
if(isset($_POST['submitcomment'])) {
$skintype = $_POST['skintype'];
$userrating = $_POST['stars'];
$userreview = $_POST['review'];
$insertreview = mysql_query("INSERT INTO itemcomments (itemid, username, commentcontent, ranking, userskintype) VALUE('$itemid', '$username',
'$userreview', '$userranking', '$skintype')");
echo "Posted!";
echo $userrating;
}
?>
While I can get the value displayed with the alert("Got it!"+ stars); I cannot get the correct value inserted into database when I click submit.
For example, I rate something for 7.5. After hitting submit I get the alert message "Go it!7.5" but in the database the value inserted is still 0. All other values ($skintype, $userreview, $itemid and $username) are inserted properly.
What might be the issue?
Thank you all for your help.

<input type="range" value="0" step="0.5" id="rating">
I think that you need to change the value to value = "" otherwise you'll always get the value zero. Try that ! And let me know. If that doesn't help than i'll look at the code again.

Related

How can I post form to mysqli without using an action page [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I don't wanna use an action page. I wanna post on the same page.
These are my codes. But there is an error called "Undefined index: baslik on line 5 and aciklama on line 6
<?php
include("baglan.php");
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik']);
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)){
echo "Güncelleme başarılı";
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>
<?php
include("baglan.php");
// check for post request here
if(isset($_POST['baslik']) && $_POST['baslik'] != "") {
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik']);
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)){
echo "Güncelleme başarılı";
}
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>
You are not checking if the POST data actually exists, so when you first load the page there is no $_POST
do something like
if (isset($_POST)){
//your php code here
}
I can see the form IS submitting to the same page.
You just should wrap the PHP code in if condition
<?php
include("baglan.php");
if(!empty($_POST['baslik'])) { // This is the new if condition
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik'] );
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)) {
echo "Güncelleme başarılı";
}
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>

trying to make feedback page [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I am making feedback page, but it is producing some errors.
i don't understand what is wrong. Please help me figure out where i am doing wrong.
when i submit the form the errors goes away and produce right result,but when i refresh the page it produces same errors again
<?php
$name = $_POST['name']; // error undefined index name
$suggestion = $_POST['suggest']; // error undefined index suggest
$opinion = $_POST['opinion']; // error undefined index opinion
$submit = $_POST['submit']; // error undefined index submit
if(isset($submit)){
$sql= mysqli_query($con,"insert into feedback (name,suggestion,opinion) values ('$name','$suggestion','$opinion')");
if($sql==true){?>
<div class="alert alert-info">
Thankyou for your suggestions. we will notify the admins.
</div>
<?php
}
}
?>
<body>
<div class="feedback">
<form action="feedback.php" method="post">
<h1> Help us improve our website</h1>
<h2>Please drop your suggestion below</h2>
<div class="form-group">
<label >Your Name:</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label for="comment">Your suggestion</label>
<textarea class="form-control" name="suggest" rows="5" id="comment" required ></textarea>
</div>
<p>Were you satisfy with this website?</p>
<label class="checkbox-inline">
<input type="checkbox" name="opinion"value="1"> yes
</label>
<label class="checkbox-inline">
<input type="checkbox" name="opinion" value="2"> no
</label>
<label class="checkbox-inline">
<input type="checkbox" name="opinion" value="3"> may be
</label>
<br /><br />
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
When initially rendering the page, it's unable to locate the index (ie. value) for $_POST array (name, suggest, etc.). That's because there is no $_POST data until you submit the form. When you submit the form, $_POST contains data, including those indexes, so it's able to successfully render the page.
You should check to see if the variable has been set or not. Something along these lines will do that:
if (isset($_POST["name"]) {
$name = $_POST["name"];
}
You can do this for each variable.
I also recommend including that within the submit condition, as it should only check for that data if the form was submitted.
Additionally, you should look into using prepared statements or input sanitization as you are setting yourself up for SQL Injection with the mysqli query.
That's because parameters passed with post method are related only to a specific page request. When you refresh the page, browsers usually ask you if you want to send forms again, otherwise the request is sent without additional data.
Your issue is exactly what the error states, your $_POST array is empty when the page is first loaded, and it is not empty (so you do not get the error) when you have POSTED the form.
replace your variable declarations as follows to make sure they are properly declared incase the $_POST array is empty:
$name = isset($_POST['name']) ? $_POST['name'] : "";
$suggestion = isset($_POST['suggest']) ? $_POST['suggest'] : "";
$opinion = isset($_POST['opinion']) ? $_POST['opinion'] : "";
$submit = isset($_POST['submit']) ? $_POST['submit'] : "";
if(isset($submit) && $submit != ""){
// your insert code here
}

PHP variable to URL in address bar [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have the code:
echo('
<div class="row">
<div class="col-sm-2" >
<form method="POST" action="plan.php">
<p>Selectati data raportului:<br>
<select name="SelectDR" onchange="form.submit()">
<option value="">Select...</option>
');
It is a form from which I select a value.
$option = isset($_POST['SelectDR']) ? $_POST['SelectDR'] : false;
if ($option){
$queryTXT = "SELECT * FROM ".$tabel." WHERE DataRaport='".$option."'";
...
The records of a SQLtable are filtered by the value selected from the form.
Now, what I want is to display in the url plan.php?DataRaport& variable php
I've tried to put in the form url (<form method="POST" action="plan.php?DataRaport'.$option.'">), but that doesn't show the variable.
You need GET not POST
<form method="GET">
First of all. If you want to echo this HTML every time, just use it as HTML:
<div class="row">
<div class="col-sm-2" >
<form method="POST" action="plan.php">
<p>Selectati data raportului:<br>
<select name="SelectDR" onchange="form.submit()">
<option value="">Select...</option>
</select>
</form>
<?php //your php stuff
To get the value from the URL, you need to change one thing:
<form method="POST" action="plan.php?DataReport=<?php echo $option?>">
Then you would get this variable as:
$dataReport = $_GET['DataReport'];
This would return the option value as POST and the DataReport as GET.
You can change the form method to GET if you wish, but you would then need to use $_GET to pick the selected option.

SESSIONS: using a user input variable for multiple php pages [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I'm trying to study php and I'm already on the sessions part where I want to input something on my first page that would then be an output for the second page
1stpage.php
<?php session_start();?>
<form method="post">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $_SESSION["$num1"];?>">
<a href ="2ndpage.php">
<input type="button" name="select" value="select">
</a>
</form>
2ndpage.php
<?php
session_start();
echo $_SESSION[$num1];
?>
Well, it does'nt work and I'm getting lots of undefined index error. Any fix guys? Thanks in advance.
Take a look at form handling:
http://www.w3schools.com/php/php_forms.asp
If you still want to save the value into a session use:
$_SESSION['num1'] = $_POST['num1'];
1stpage.php
<?php session_start();?>
<?php if(isset($_SESSION['num1'])) $num1 = $_SESSION['num1']; else $num1 = ''; ?>
<form method="post" action="2ndpage.php">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $num1;?>">
<input type="submit" name="select" value="select">
</form>
2ndpage.php
<?php
session_start();
$_SESSION['num1'] = $_POST['num1'];
echo $_SESSION['num1'];
?>
What I have done here is I have first looked into the session if the num1 offset has been set into the session. If its not then I give $num as blank else I set the assign the value from session to $num.
Now when we input something and post it on 2nd page, the posted value is assigned to the variable in session and is displayed as well. So that next time you visit 1stpage.php while in same session, you will see your last posted value.
Let us know if it solves your practive problem or if this is not what you wanted.
I believe this is what you're trying to do:
Page 1:
<?php
session_start();
if(isset($_POST['select'])){
$num1 = $_POST['num1'];
$_SESSION['num1'] = $num1;
}
?>
<form method="post">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $_SESSION['num1']; ?>">
<input type="submit" name="select" value="select">
Click here to See the Value
</form>
Page 2:
<?php
session_start();
echo $_SESSION['num1'];
?>
Where, at first you press the select to enter the value inside
num1 then click on Click here to See the Value to see it.
I don't think you understand how all this works. You're having PHP output a session variable into a text box and expecting that that text box will somehow magically update your session. Somewhere you have to pass that data back to PHP and update it. So let's make your code into a process
<?php session_start(); ?>
<form method="post" action="process.php">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $_SESSION["$num1"];?>">
<input type="submit" name="select" value="select">
</form>
What I've done is make this form do a basic POST. If you want to avoid this you can use AJAX instead (same result using JS instead of HTML)
Next, let's make a basic process.php intermediate page
<?php
session_start();
$_SESSION['num1'] = (int)$_POST['num1'];
header('Location: 2ndpage.php');
Now we have a logistics chain for the data. You accept the data in your HTML and POST it to this page. It then takes it, sanitizes it (note I cast it to an (int) so we are certain this is a number) and then issues a redirect to the browser which takes you to your original landing page, which should now work.
This is all oversimplified for the process of teaching. You could have your form submit directly to 2ndpage.php and process there. Or, as I said before, you could use AJAX to send to process.php and when it returns some success parameter you then use JS to redirect. You have many options here.
Put this on the first page:
$_SESSION['num1'] = $num1;
and this on the second
$num1 = $_SESSION['num1'];

PHP error: Undefined index: submit [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
In this once the submit button is clicked it retrieve the data from database. But once i enter this page Undefined index: submit is shown before i click the submit button.
<div id="title">
<form method="get" action="<?php echo $_SERVER['PHP_SELF']?>">
<h3>Select your product and click submit</h3><br />
<select name="prod">
<option value="default">Select your product</option>
<option value="os">Operating Systems</option>
<option value="mobile">Smart Mobiles</option>
<option value="mobile">Computers</option>
<option value="shirt">Shirts</option>
</select><br /><br />
<input type="submit" value="Submit" name="submit"/>
</form>
The php code is:
<?php
$submitcheck=$_GET['submit'];
echo $submitcheck;
if (!isset($submitcheck)) {
echo 'Pls select and submit';
} else {
....
}
?>
You need to change your PHP to:
if (isset($_GET['submit']))
{
// Form has been submitted
}
else
{
// Form has not been submitted
}
At the moment, you're assigning a value which might not exist to $submitcheck and then checking whether it's set. You need to do it the other way round: check $_GET['submit'] is set, then assign it to a variable.
It's bacause you code is executed from top to bottom and on the top you are using $_GET['submit'] and then this key does not exist. You could do something like that:
if(array_key_exists('submit', $_GET)){
echo $_GET['submit'];
}
or even
if (!isset($_GET['submit']))
{
echo 'Pls select and submit';
}
else
{
....
}

Categories