1

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>

avghdev
  • 89
  • 8

1 Answers1

2

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]);
}

?>
Asheesh
  • 161
  • 3
  • 16
  • It worked! If you could explain why that works that would be awesome :) btw I'll accept your answer when it lets me (in 5 minutes). Thank you!!! – avghdev May 02 '16 at 08:20
  • 1
    use header function in php why you use javascript – Sanooj T May 02 '16 at 08:23
  • sanooj.. http://stackoverflow.com/questions/21226166/ http://stackoverflow.com/questions/423860/ http://stackoverflow.com/questions/1242033/ easy for work. – Asheesh May 02 '16 at 08:26
  • you should use both (http redirect and client-side js redirect) if you want to be absolutely sure it works – strangeqargo May 02 '16 at 08:32