GenGen GenGen - 1 year ago 67
PHP Question

Ajax not fully posting data to php file

Getting a really strange error with Ajax not sending the entire string data across to the php script.

The string is

"<p class="MsoNormal" style="text-align:justify"><b><u><span lang="DE" style="font-size:10.0pt;mso-bidi-font-size:11.0pt;
font-family:&quot;Verdana&quot;,sans-serif;mso-ansi-language:DE">Gold GoldGold Gold Gold Gold<o:p></o:p></span></u></b></p>

<p class="MsoNormal" style="text-align:justify"><span lang="EN-GB" style="font-size:10.0pt;mso-bidi-font-size:11.0pt;font-family:&quot;Verdana&quot;,sans-serif">&nbsp;</span></p>"

but what arrives at the php script is only (Seen through DB submission and the php $result test.

"<p class="MsoNormal" style="text-align:justify"><b><u><span lang="DE" style="font-size:10.0pt;mso-bidi-font-size:11.0pt;

I have tried to check through multiple methods of why this is happening but just cant figure it out at all.

Here is my Javascript code and php code.


function Send_upload_news()
get_title = document.getElementById('Germ_title');
get_date = document.getElementById('Germ_date');

input_title = get_title.value;
input_date = get_date.value;
input_content = $("div.nicEdit-main").html();


var Data = '&input_title='+input_title+'&input_date='+input_date+'&input_content='+input_content;
url : "uploadNews.php",
type: "POST",
dataType: 'text',
data : Data,
success: function(result){alert(result);},
/* success: function() {
alert('Post has been uploaded to Database');
}, */
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('There is an error, screenshot this error and send to Admin : ' +textStatus+" - "+errorThrown);
nicEditors.findEditor( "Germ_content" ).setContent( '' );
get_title.value = "";
get_date.value = "";
$('html, body').animate({ scrollTop: 0 }, 'slow');



//Database info
$db_host = "localhost";
$db_username = "";
$db_pass = "";
$db_name = "";
//connect db
$connMS = new mysqli ( $db_host, $db_username, $db_pass, $db_name );
//grab data

$this_title = $_POST['input_title'];
$this_date = $_POST['input_date'];
$this_content = $_POST['input_content'];

$result = file_put_contents ( "test.txt", $this_content);
//create query and execute
$sqlupdate_news = "INSERT into news_content(germ_title, germ_date, germ_content) values ('$this_title','$this_date','$this_content')";
mysqli_query ($connMS,$sqlupdate_news);

Im using WYSWYG Nicedit as my text area

If there is somebody that can help me figure this out I would be very grateful.

Answer Source

You have to escape(encode) sent parameters to the server, to avoid confusion of characters like ampersand(&)

Change this:

var Data = '&input_title='+input_title+'&input_date='+input_date+'&input_content='+input_content;

To this:

var Data = '&input_title='+encodeURIComponent(input_title)+'&input_date='+encodeURIComponent(input_date)+'&input_content='+encodeURIComponent(input_content);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download