0

i need help with a form submit.

here is my code

<script type="text/javascript" src="js/jquery-1.7.2.js"></script>

    <script type="text/javascript">  
  $(document).ready(function(){  
    $("#form").submit(function(event) {  

        /* stop form from submitting normally */  
        event.preventDefault();   

        $.post( \'clase/app/add_meniu.php\', $("#form").serialize(),  
          function( data ) {  
              $("#status").append(data);  
          }  
        );  
      });  
  });  
</script>  

and here is the html

<form id="form" method="post" accept-charset="UTF-8" >
         <fieldset>
<p>
        <label>Denumire</label>
        <input class="text-input small-input" type="text" name="text"   maxlength="200" value="">
    </p>
<p>
        <label>Link</label>
        <input class="text-input small-input" type="text" name="link"  maxlength="200" value="">
    </p>

here is the add_meniu.php code

   require_once('../core/db.php');
$text=$_POST['text'];
    $link=$_POST['link'];
    $limba=$_POST['limba'];
echo'<div class="notification success png_bg">
                <a href="#" class="close"><img src="resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
                <div>
                    Meniul '.$text.' a fost adaugat cu succes
                </div>
            </div>';

$query = "Insert into Meniu (Denumire,Link,ID_Limba)  VALUES ('".$text."','".$link."','".$limba."')";
$result = mysql_query($query)
or die("query failed: " . mysql_error());

The thing is that in database when i insert info's from the external file "add_meniu.php" it doesnt insert it with UTF-8. I have my page, form and mysql table utf-8 , can you pls give me a solution?

  • Is your .php file in UTF-8 format without BOM? You can use Notepad++ to check this. – Marcio Mazzucato Oct 30 '12 at 13:23
  • Is this `\'clase/app/add_meniu.php\'` your current url? – Ram Oct 30 '12 at 13:25
  • You are using [an obsolete database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also exposing yourself to [SQL injection attacks](http://bobby-tables.com/) that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Oct 30 '12 at 13:29
  • problem has nothing to do with ajax submit. Read AJAX docs the default content type is : `application/x-www-form-urlencoded; charset=UTF-8` http://api.jquery.com/jQuery.ajax/ – charlietfl Oct 30 '12 at 13:29
  • "it doesnt insert it with UTF-8." — What *does* it do? How can you tell it isn't UTF-8? – Quentin Oct 30 '12 at 13:29
  • Quentin i resolved it, thnx for the tip with mysql, i'll change my code to mysqli – Sima Cristian Alexandru Oct 30 '12 at 13:38

3 Answers3

1

Firstly, it is strongly recommended that you check if your PHP file has the UTF-8 format without BOM, you can see this easily using Notepad++ and convert it if necessary.

Other points i recommend to check:

HTML

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

PHP

ini_set( 'default_charset', 'UTF-8' );
mb_internal_encoding('UTF-8');

MySQL

mysql_query("SET NAMES 'utf8'");
Marcio Mazzucato
  • 8,841
  • 9
  • 64
  • 79
0

Likely, its your DB then.

mysql_query("SET NAMES utf8");
wesside
  • 5,622
  • 5
  • 30
  • 35
0

If you are speaking about data inserted in your mysql database, you have to set how mysql will display data.

Use the following query when just after setted your db connection in php file:

mysql_query("SET NAMES 'utf8'");

This as to be done on each connection as its relative to db session.

A. Wolff
  • 74,033
  • 9
  • 94
  • 155