0

I have a website where one can post stories. There is a form for that, and when a user submits the form, there must be a check whether a story with identical title exists - if yes, then prevent form from submitting. The form and check function:

if (!isset($_SESSION[username]))
   echo "<br>You are not logged in";
else echo "
   <form method='get' action='storyupload2.php' onsubmit='return checkTitle(); return false; '>
      <input id='titleInput' type='text' name='title' placeholder='Title (maximum 200 characters)'  maxlength='200' size='200' required><br>
   <textarea name='story' placeholder='Story (you can use HTML tags)' rows='33' cols=200 maxlength='10000' required onkeyup=countchars(this)></textarea>
      <div id='counter'></div>
      <input type='submit' value='Post'>
      <span id='error'></span>
   </form>"
?>

function checkTitle() {
      var title=document.getElementById('titleInput')
      var value=title.value;
      var result=false
      var request=new XMLHttpRequest()
      request.onreadystatechange=function () {
         if(request.readyState==4 && request.status==200)
         {
            result=JSON.parse(request.responseText);
            console.log(result)
            if(result==true)
            {
               var span=document.getElementById('error')
               span.innerHTML='Story with such title exists. Write a different title';
               return false;
            }
                return true
         }
      }
      request.open("GET",'checktitle.php?t='+value,true)
      request.send()
   }

The problem is that the function throws SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data error. But if I remove 'return' so it is just onsubmit='checkTitle(); return false;', the function works properly. The php file performs the check:

<?php
include 'constants.php';
$storytitle=$_GET['t'];
//echo $storytitle."\n";
$con=new mysqli('',databaseuser,databasepassword,database);
    $q="SELECT title FROM Stories";
    $r=$con->query($q);
    while($row=$r->fetch_array())
    {
        $existingtitle=$row[0];
        if($existingtitle==$storytitle)
        {
            echo json_encode(true);
            exit();
        }

    }
    echo json_encode(false);

I have searched for solutions for a long time, and haven't figured it out.

Alexiy
  • 1,966
  • 16
  • 18
  • 1
    before `echo json_encode`, try putting `header('Content-Type: application/json');` – Jigar May 01 '16 at 07:23
  • And exit after echo to ensure your response is sent right after echo. – SmasherHell May 01 '16 at 07:30
  • @Alexiy why do you `json_encode` boolean values? just echo "0" or "1" then check result=="0" or "1" – TRiNE May 01 '16 at 07:47
  • @Jigar I added that, but it didn't help. But when I added `&& request.status==200`, it got rid of the error, though the form submits always anyway. – Alexiy May 01 '16 at 10:16
  • http://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted – strangeqargo May 01 '16 at 12:25
  • Nevermind, I've found out that returning from asynchronous functions is impossible, so I had to set 'disableDefault' and submit form via Javascript. – Alexiy May 01 '16 at 15:08

0 Answers0