1

I have a problem in PHP/Jquery.

What I want : I have a page in PHP that I use to change article in my website. The page contain text and textarea in PHP plus 1 button. When I enter that page, the page fill any text form and text area automatically from database. I use that form to write some article that contain about 200-800 character. The article contain some Line breaks using Enter Key. When I press My save button, the text in textarea and text forms was store in SQL database with VARCHAR/TEXT type to replace the OLD article. My Save button just reload the same page and save the text in textarea using PHP. I just successfully make that dynamically so when I press the save button, the text in textarea and any other form doesn't change back to old article.

The problem : When text in textarea have a line breaks from enter key (not from wordwrap), the text didn't want to fill any text form and textarea when I access the page from save button or just from other page. So my text in form suddenly disapear when I reload the page. But the text successfully save with the line breaks too in SQL when I check with PHP my admin. This problem doesnt came out when I write without any line breaks.

This is PHP code that handle saving and reading database.

$judul = $_POST['inputEditJudul'];
$jenis = $_POST['inputEditJenis'];                            
$isiArt = $_POST['inputEditIsi'];
date_default_timezone_set('Indonesia/Jakarta');
$tanggal  = date('Y-m-d');

if(isset($_POST['btn_Save'])) {
   $sql = "UPDATE artikel
           SET judul='".$judul."', jenis_artikel='".$jenis."', isi_artikel='".$isiArt."', tanggal= CAST('".$tanggal."'AS DATE)
       WHERE id=".$_GET['id'].";";
   mysql_query($sql,$link);
 }

$sql = "SELECT * FROM artikel WHERE id=".$_GET['id'].";";
$result = mysql_query($sql, $link);
$row = mysql_fetch_array($result);

This is the jquery script that handle filling the form in same page.

<script>
$(document).ready(function(){
   $("#inputEditJudul").val("<?php echo ($row['judul']) ?>");
   $("#inputEditIsi").val("<?php echo nl2br($row['isi_artikel']) ?>");

   var jenisArt = <?php echo $row['jenis_artikel'] ?>;
   if ( jenisArt == 1){
     $("#inputEditJenis").val("1");
   } else {
     $("#inputEditJenis").val("2");
   };
});
</script>

This is the text area in my form

....    
<div class="form-group">
        <label for="inputEditIsi">Isi Artikel</label>
        <textarea class="form-control" id="inputEditIsi" name="inputEditIsi" rows="10" maxlength="800"></textarea>
    </div>
....

If possible, please just use PHP/Jquery. I dont know anyhing about ajax.

Sorry for my bad English. I'm still newbie in here.

Thank you

1 Answers1

1

The problem is that JavaScript doesn't "like" line breaks.

var foo = "hello world";

will work fine, however

var foo = "hello
           world";

won't and you'll get an error in the developers console, such as

Uncaught SyntaxError: Unexpected token ILLEGAL

nl2br will add proper <br> but leaves the linebreaks and so your JavaScript is invalid.

As you aren't using AJAX right now, what is wrong with writing the content directly into the textarea?

<textarea class="form-control" id="inputEditIsi" name="inputEditIsi" rows="10" maxlength="800">
<?php echo $row['isi_artikel']; ?></textarea>

If you can't for some reason, take a look at other questions concerning Removing line breaks from string. With such a solution applied your JavaScript should once again be valid.

Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51