1

I'm a beginner in php so there might be alot wrong with this code but I really need some help!

I'm making an textarea for users to post to users walls. Everything works fine and the form is submitting to the database the only thing that goes wrong is that the text line undefined get added to the end of the input text both in the textarea and in the database. I most have forgotten to assign something but what?

my html code:

<form action="" method="POST">
    <textarea id="message" name="pinlagg" rows="4" placeholder="Inlägg:"onfocus="this.placeholder = ''" onblur="this.placeholder = 'Inlägg:'" ></textarea>
    <input type="submit" class="button" value="Skicka"/>
</form>

my php code:

if (isset($_POST['pinlagg'])) {
$session_user_id = $_SESSION['id'];
$username = $user_data['userName'];
$pinlagg = $_POST['pinlagg'];
$mysql['pinlagg'] = mysql_real_escape_string($clean['pinlagg']);

$sql="INSERT INTO profile_post(post, username, added) VALUES('$pinlagg', '$username', '".date("Y-m-d H:i:s")."')";
mysql_query($sql);
} else {
    echo 'Du har inte skrivit något!';
}

My database has 4 columns. id, post(varchar), username(varchar) and added(timestamp).

I also have a problem with getting the local time from timestamp.

js for including emojis:

$(document).ready(function(){

$('.button').click(function(){

    var smiley = $(this).attr('smiley');
    var message = jQuery.trim($('#message').val());

    if (message == '') {
        var sp = '';
    } else {
        var sp = ' ';
    }

    $('#message').focus().val(jQuery.trim(message + sp + smiley + sp))
});
    $('#message').keyup(function(e){
    if (e.keyCode == 13) {
        var message = $(this).val();

        $.post('replace.php', {message: message}, function(data){
            $('#display_message').append(data + '<br/><br/>');
        });
    }
});
});

php for including emojis:

<?php
$message = $_POST['message'];

$message = str_replace(':)', '<img src=./emoji/glad_face.png', $message);
$message = str_replace('%D', '<img src=./emoji/drunk_face.png', $message);
$message = str_replace(';)', '<img src=./emoji/winky_face.gif', $message);
$message = str_replace(';P', '<img src=./emoji/ha_face.gif', $message);
$message = str_replace(':(', '<img src=./emoji/sad_face.png', $message);
$message = str_replace('X(', '<img src=./emoji/offended_anim.gif', $message);
$message = str_replace(';(', '<img src=./emoji/crying_anim.gif', $message);
$message = str_replace(':/', '<img src=./emoji/wtf_face.png', $message);
$message = str_replace(':r', '<img src=./emoji/rant_face.gif', $message);
$message = str_replace(':o', '<img src=./emoji/ohno_face.png', $message);
$message = str_replace('<3', '<img src=./emoji/love.png', $message);
$message = str_replace('<}3', '<img src=./emoji/broken.png', $message);
$message = str_replace('(Y)', '<img src=./emoji/like.png', $message);
$message = str_replace('(N)', '<img src=./emoji/dislike.png', $message);
$message = str_replace('}-', '<img src=./emoji/fu.png', $message);
$message = str_replace(':x', '<img src=./emoji/kiss_anim.png', $message);

echo $message;
?>
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
BriefHeart
  • 11
  • 1
  • 5
  • Switch to mysqli or PDO since mysql is deprecated (http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – daxro Feb 08 '16 at 21:33
  • Btw: removing `onfocus="this.placeholder = ''" onblur="this.placeholder = 'Inlägg:'"` doesn't change anything? – daxro Feb 08 '16 at 21:38
  • Okej so I switched the lines that says mysql_* to mysqli_* but the problem is still there – BriefHeart Feb 08 '16 at 21:40
  • No I'm afraid the of and on focus didn't matter. It's when I hit the submit button the text appears in the textarea – BriefHeart Feb 08 '16 at 21:43
  • I cannot reproduce this. Do you include some scripts in your HTML? – trincot Feb 08 '16 at 21:57
  • Yes I'm working with foundation. And for this page I also include a script I've written to include emojis. – BriefHeart Feb 08 '16 at 22:11

0 Answers0