avghdev avghdev - 7 months ago 8
PHP Question

Stop PHP from making repeat querys on refresh

I'm making a web application, and I'm having trouble figuring out how to stop the PHP from making a query to my database every time the page is reloaded.

Basically when the form is submitted I want the PHP to make a query to the database adding the variables, however, because it has saved those values in the $_POST, it's making a query every time I refresh with those same values.

I want it to make the query, then perhaps unset the $_POST values or something so that it doesn't satisfy the if condition, and in turn stops it from querying the database with repeat values when the submit button hasn't updated them with new values.

Sorry if that's convoluted I'm trying to explain my problem the best I can.

Here is the PHP

<?php

//Require login gile -- Fatal error if not found
require_once('login.php');
require_once('app.html');

//Connect w/ MySQL database [login variables]
$con = new mysqli($hn, $un, $pw, $db);

//If connection error -> kill application and display error
if ($con->connect_error) die('Connect Error (' . $con->connect_errno . ') '. $con->connect_error);

//If both goal and difficulty are set
if (!empty($_POST['goal']) && !empty($_POST['difficulty'])) {
$goal = get_post($con, 'goal');
$difficulty = get_post($con, 'difficulty');
$query = "INSERT INTO items VALUES" . "('$goal', 1, '$difficulty', NULL)";
$result = $con->query($query);
if(!$result) echo "INSERT failed: $query<br>" . $con->error . "<br><br>";
}

unset($_POST['goal']);
unset($_POST['difficulty']);


//close connection
$con->close();

function get_post($conn, $var) {
return $conn->real_escape_string($_POST[$var]);
}

?>


html

<!DOCTYPE html>
<html>
<head>
<title>Testing</title>
<!--Page Style-->
<link type="text/css" rel="stylesheet" href="Style.css">
<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<!--RateYo | http://prrashi.github.io/rateYo/-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.0.1/jquery.rateyo.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.0.1/jquery.rateyo.min.js"></script>
</head>
<body>
<div id="parent1">
<form id="goalForm" method="post" action="PHP.php">
<!--Goal Value-->
<input id="goalName" type="text" name="goal" maxlength="150" placeholder="Type your goal here!">

<!--Star Rating-->
<div id="rateYo"></div><br>

<!--Value from stars to be submitted-->
<input id="dif" type="hidden" name="difficulty">

<input class="but1" id="firstSubmit" type="button" value="Submit">
<input class="but1" id="submitton" type="submit" style="display:none" value="Difficulty?">
</form>
</div>
<script>

$("#goalName").val("");


//First Submit Onclick
$("#firstSubmit").click(function() {
if($("#goalName").val() == "") {
alert("Please insert a value.");
}
else {

$("#goalName").fadeOut("slow" , function() {
$("#rateYo").rateYo({
numStars: 10,
rating: 0,
fullStar: true,
starWidth: "70px",
ratedFill: "#E74C3C",
maxValue: 10
});

$("#rateYo").fadeIn("slow");
});

$(this).hide();
$("#submitton").show();
}
});

//Star Submit
$("#goalForm").submit(function() {

var $rateYo = $("#rateYo").rateYo();
var rating = $rateYo.rateYo("rating");

if($("#goalName").val() == "") {
alert("Please insert a value.");
return false;
}
else if(rating == 0) {
alert("Please set a difficulty level.");
return false;
}
else {
$("#dif").val(rating);
}



});


</script>




Answer

Try it plz

<?php  

//Require login gile -- Fatal error if not found
require_once('login.php');
require_once('app.html');

//Connect w/ MySQL database  [login variables]
$con = new mysqli($hn, $un, $pw, $db);

//If connection error -> kill application and display error
if ($con->connect_error) die('Connect Error (' . $con->connect_errno . ') '. $con->connect_error);

//If both goal and difficulty are set 
if (!empty($_POST['goal']) && !empty($_POST['difficulty'])) {
    $goal = get_post($con, 'goal');
    $difficulty = get_post($con, 'difficulty');
    $query = "INSERT INTO items VALUES" . "('$goal', 1, '$difficulty', NULL)";
    $result = $con->query($query);
    if(!$result) echo "INSERT failed: $query<br>" . $con->error . "<br><br>";
    //You can write samepage name in location as window.location='abc.php'
    echo "<script>window.location=''</script>";
}

//close connection
$con->close();

function get_post($conn, $var) {
    return $conn->real_escape_string($_POST[$var]);
}

?>