Ajax is adding enter sign to text from DB

I am using ajax to get data fro DB

 success: function(strdate){     
         whichToSave=strdate;

Open in new window


 $query = "SELECT last FROM users  WHERE userName='$weGotTheuser'";

Open in new window


I am getting  "enter" sign from querry.

Where is it coming from. In DB i have just "B57"?

Check the attached picture?
Untitled11.png
Ivan GolubarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mohan singhWeb developerCommented:
Please Can you elaborate your problem  
what you want ..?
0
Ivan GolubarAuthor Commented:
I did mark on attached picture with red color  variable which I am getting from DB inside ajax success function . Variable has 3 characters (B57) + enter sighn. Enter sign is what i don't want. I think i have to find out where "enter" is coming from, to neutralize it.

When i click on a field in data base, cursor gets position next to last character (as it happens when you click in input field element), that's I may see vertical line (check the green circle on attached picture) then. But i can't believe that this is the origin of enter sign.
Untitled11.png
0
Ivan GolubarAuthor Commented:
So, maybe i could use some function which works with strings to filter the text. To find out "enter" sign and cut it out from variable. But i would prefer to understand what is going on.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Olaf DoschkeSoftware DeveloperCommented:
You only show how you retrieve the ajax return value, you don't show how you return the query result from PHP to the ajax caller. Your PHP output might add this ENTER.

Since PHP outputs headers before the raw response you have more output than you control anyway and the response body might also always end in a newline. Simply try a PHP script returning nothing, whether you get an ENTER from that.

A typical solution to not depend on such raw output unaltered or uninterpreted is going for XML or JSON output, which may seem unfortunate for requests only intending to return a 3 character short string, but the HTTP protocol traffic is much higher for this net return anyway.

Edit: Just a simple afterthought: That enter might even just be a newline after the closing ?> in your PHP script. Any part of a PHP script before <?php and after ?> is unaltered output, so that newline might not even be a default HTTP protocol end character arriving within the ajax return value and the parameter of the success function, it might simply be a stray newline of your PHP script end.

Edit2: If you find and eliminate this newline and it still arrives, it could be stuck in the browser cache.

Bye, Olaf.
0
Ivan GolubarAuthor Commented:
Next is ajax to send  options from select element to php
function saveToProjectsListF(){  
  saveToprojectsList=document.getElementById("selectProjectID");

var optionVal = new Array();
for (i = 0; i < saveToprojectsList.length; i++) { 
    optionVal.push(saveToprojectsList.options[i].text);
}
var jsonProjectsListToPHP= JSON.stringify(optionVal);
 console.log(jsonProjectsListToPHP);
   $.ajax({
   method:"POST",
   url: '/wp-content/themes/4/PsaveJson.php',
   data: {
     "sendProjectsList":1,
       "weGotTheuser":weGotTheuser,
    "jsonProjectsListToPHP":jsonProjectsListToPHP
     },
    success: function(data){
    }
   });   
}

Open in new window



  Next is ajax which reads  data  from db to select element
function  getProjectsListF(){  //___________________________________________________________ getProjectsListF(){
 $.ajax({
  method:"POST",
  url: '/wp-content/themes/4/PgetJson.php',
  data:  {
      "getProjectsList":1,
       "weGotTheuser":weGotTheuser
   },
   datatype: "text",
    success: function(strdate){
       selectbox= document.getElementById("selectProjectID");
         var i;
    for(i = selectbox.options.length - 1 ; i >= 1 ; i--)
    {
        selectbox.remove(i);
    }
     var projectsListFromDB =JSON.parse(strdate);
    // selectElement.options.length = 0;
     for(var i = 0; i < projectsListFromDB.length; i++) {
       var opt = document.createElement('option');
       opt.innerHTML = projectsListFromDB[i];
       opt.value = projectsListFromDB[i];
       selectElement.appendChild(opt);
      }
     console.log("ProjectsList "+ strdate);
   getProjectsListForUserF();
     },
     error: function(error, txtStatus) {
      console.log(txtStatus);
      console.log('error');
    }
  });
}

Open in new window


Next php which  reads from DB
if (isset($_POST["getProjectsList"]) ) { 
      $ProjectsListForUser = array();
          $weGotTheuser= mysqli_real_escape_string($db2, $_POST['weGotTheuser']);
          $sql = "SELECT projectName FROM projectObjectstable WHERE creator='$weGotTheuser' ";
          $query = mysqli_query($db2, $sql);
        while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        $ProjectsList[] = $row["projectName"];
        }
         echo json_encode($ProjectsList);
       }

Open in new window


and next one writes to DB
if (isset($_POST["sendProjectsList"]) ) {
                          $username= mysqli_real_escape_string($db5, $_POST['weGotTheuser']);        
                         $projectsList= mysqli_real_escape_string($db5, $_POST['jsonProjectsListToPHP']);
                         $sql="ALTER TABLE users ADD UNIQUE project_object_unique (username)";
                         $sql2="INSERT INTO  users (username,email,password,projectsList,lastOpenedProject)
                                           VALUES('$username','$email','$password','$projectsList','$whichProjectToSave')
                                           ON DUPLICATE KEY
                                           UPDATE projectsList='$projectsList'";
                         
                         mysqli_query($db5,$sql);
                         mysqli_query($db5,$sql2);                         
                          }

Open in new window

0
Julian HansenCommented:
I am guessing you are talking about data having a Carriage Return (new line character) after it.

The PHP above seems to be getting data from table projectObjectstable where as your opening query is against table users.

Firstly are you 100% sure the DB value does not have a CR on the end

Try running this against your table
UPDATE users SET userName = REPLACE(userName, '\r','');

Open in new window

Then
UPDATE users SET userName = REPLACE(userName, '\n','');

Open in new window

Then try your AJAX request again.
NB: I do not have access to your system so I cannot guarantee the above queries exactly match your environment. Please ensure table names and field names are correct.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Olaf DoschkeSoftware DeveloperCommented:
Seems you're posting the code you now advanced using the idea to use json for data transfer, as you changed from
whichToSave=strdate

Open in new window

to
var projectsListFromDB =JSON.parse(strdate);

Open in new window


And as Julian already noticed, your code now queries the projectObjects table instead of the users table.

The JSON you output in
echo json_encode($ProjectsList);

Open in new window

will now contain something like
{"projectName":"B57"...

Open in new window

and if your data really contains linefeeds you'll have a "B57\n", and if there is some extra linefeed after the PHP end tag I mentioned as another possibility, it's outside of that JSON and won't affect the parsing of it.

So is the problem solved? Or do you still struggle with linefeeds within the string within the JSON strdate now must be?

Bye, Olaf.
0
Ivan GolubarAuthor Commented:
Sorry i made some confusion. I am not using  table  users any more.
It is now already one year that i am writing this code and I am beginning to notice that  I am forgetting also some "bigger things" .

Julian Hansen i did try you suggestions directly in phpMyAdmin  as Run SQL query/queries  with my table and variable. But i did not notice any difference after that. Sorry again.

Amway as i was saying i did add  also this to my code and i did forgot about it.
Seams that  &#8592; sign is what troubles me.  And i think that  it would be better for me to cut it out than study 400 lines (just one specific  *.js) of code.

So i think i will have to use this not elegant solution in this case. Can you suggest me a function where i could use directly &#8592; as parameter which i would like to cut out from string?

Something like: cutOut(myString, &#8592;);
Or something similar

And i hope that this will be the solution .

 <div id="projectModalID" class="projectModalC">
                              <div class="projectModalC-conten">
                                 <span class="closeProject">&times;</span>
                                 <p>Project window settings</p>
                                   <div id="projectModal0ID">
                                  <input type="button" id="addNextProjectID" value=" Add "  title="You are adding it"/>
                                   <span>&#43;</span>&nbsp;<span>&#8594;</span><br><br>
                                 </div>
                                   <table id="projectsTableID" >
                                  <tr>
                                      <th ></th>
                                    <th align="left" >Projects list</th>
                                  </tr>
                                 </table>
                                 <div id="projectModal1ID">
                                    <span>&#8594;</span>
                                    <input type="button" id="archiveProjectID" value="Archive "  title="You are moveing it to archive"/>
                                    <span>&#8594;</span><br><br>
                                    <span>&#8592;</span>
                                    <input type="button" id="retriveProjectID" value="Retrive "  title="you are moveing it from archive" />
                                    <span>&#8592;</span>
                                 </div>
                                  <table id="archiveTableID" >
                                  <tr  >
                                    <th ></th>
                                    <th align="left">Archive list</th>
                                  </tr>
                                 </table>
                                   <div id="projectModal2ID">
                                     <span>&#8594;</span> &Oslash;   
                                  <input type="button" id="deleteProjectID" value=" Delete" title="delete from list"/>
                                 </div>
                              </div>
                            </div>

Open in new window

0
Olaf DoschkeSoftware DeveloperCommented:
The code you showed sets options in an element with id selectProjectID, the div with the &#8594; has the id projectModalID, so lookout for ajax or PHP outputting that part of the HTML, if it's not simply hardcoded.
If it's not coming from data you can get rid of it as simple as search&replace in any editor. If you have it in data MySQL also has a REPLACE function.

Bye, Olaf.
0
Ivan GolubarAuthor Commented:
Now I  suspect  that <br /> is couse of my troubles.
Sorry to  mislead you through this  question .

I will give up and use next to get to solution.

 
whichProjectToSave=whichProjectToSave.replace(/\s+/g, ' ').trim();

Open in new window

0
Ivan GolubarAuthor Commented:
Why I can not see buttons for assign points in this topic?
0
Olaf DoschkeSoftware DeveloperCommented:
I just learned from the comment of a poster of another question, that the system to assign points changed recently. Take your time finding out, but only the poster of a question will see this anyway,

Bye, Olaf.
0
Ivan GolubarAuthor Commented:
It is confusing me , because i can see just best solution button and not the assisting solution.
0
Olaf DoschkeSoftware DeveloperCommented:
Could you be asked for assists after picking best?
0
Ivan GolubarAuthor Commented:
No.


I have to leave now for weekend.
Probably I will check again this evening.
0
Olaf DoschkeSoftware DeveloperCommented:
Have a nice weekend.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.