0

I cannot find anywhere, this is unique problem I think.

I want to add grade ending with plus such as B+, C+ or D+. I can added A-, B-, C- and D- or any other grade A,B,C,D,F. However, when I want to add variable ending with plus it is disappeared. Can someone see any problem in my code.

Small part of index.php

var subjectone = $('#selectedsubjectone :selected').val();
var courseone = $('#courseListone').val();
var gradeone = ($('#selectedGradeOne :selected').val());

var div = document.getElementById("dom-target");
var username = div.textContent;
username = username.trim().replace(/ /g, '%20');

if(gradeone != 'gradesvalue'){
  var enrolledone ="username="+username+"&subject="+subjectone+"&grade="+gradeone+"&course="+courseone;          //I CHECK THERE IS NO PROBLEM HERE. IT SHOWS WITH PLUS.
       $.ajax({
         type: "POST",
         url: "updatecourse.php",
         data: enrolledone,
         success: function(data) {}
       });

There are two similar php file one of them for insert one of them for update. It is updatecourse.php

<?php
include_once "connection.php";

if(isset($_POST["username"]) && isset($_POST["subject"]) && isset($_POST["course"]) && isset($_POST["grade"])){

    $nick = urldecode($_POST["username"]);
    $subject=urldecode($_POST["subject"]);
    $course=urldecode($_POST["course"]);
    $grade=urldecode($_POST["grade"]);
    echo "$nick - $subject - $course - $grade";  //IT SHOWS B, NOT B+ IN HERE.
    $prep = $con->prepare("UPDATE enrolledtable SET grade=? WHERE nickname=? AND subject=? AND course=?");

    $prep->bind_param("ssss", $grade, $nick, $subject, $course);
    $send = $prep->execute();

    if ($send == TRUE) {
        echo "Courses added successfully";
        //header('Location: index.php');
        exit();
    } else {
        echo "Error: " . $con->error;
        //header('Location: index.php');
        exit();
    }
  }?>

In php part B+ becomes B, C+ becomes C. What is the problem in php? Or should I change data type in js.

ozan
  • 33
  • 1
  • 9

3 Answers3

1

The problem you are facing is that + in queries are interpreted as spaces. Simply use JSs encodeURIComponent() to encode your grades.

var subjectone = $('#selectedsubjectone :selected').val();
var courseone = $('#courseListone').val();
var gradeone = ($('#selectedGradeOne :selected').val());

var div = document.getElementById("dom-target");
var username = div.textContent;
username = username.trim().replace(/ /g, '%20');

if(gradeone != 'gradesvalue'){
  var enrolledone ="username="+username+"&subject="+subjectone+"&grade="+encodeURIComponent(gradeone)+"&course="+courseone;          //I CHECK THERE IS NO PROBLEM HERE. IT SHOWS WITH PLUS.
       $.ajax({
         type: "POST",
         url: "updatecourse.php",
         data: enrolledone,
         success: function(data) {}
       });
  • Thanks for your effort. But same problem continues. Can you edit enocodeURIComponent with encodeURIComponent. – ozan Dec 03 '17 at 17:34
  • Right, I fixed the typo. Does this still give you just "B "? –  Dec 03 '17 at 17:58
  • Yes. `$grade=urldecode($_POST["grade"]);` is wrong. I change that with `$grade=$_POST["grade"]; ` – ozan Dec 03 '17 at 17:59
1

What happens if you change this:

$grade=urldecode($_POST["grade"]);

to this:

$grade=$_POST["grade"]; ?

Since a plus sign is a urlencoded space, and $_POST variables don't need decoding I'm guessing you are stripping it out by accident here

miknik
  • 5,748
  • 1
  • 10
  • 26
0

Use JavaScript function encodeURIComponent to encode your variable values.

TurtleTread
  • 1,297
  • 2
  • 12
  • 23