Hi stackoverflow community i don't know what is wrong, I have two pages one is index.php and another is test.php am using JQUERY to post data from index.php page to test.php page but when include "&" in textarea value, empty data posted to test.php. Below are my code.
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<title>test</title>
</head>
<body>
<!----- This is my data that i type in the textarea=>'& <a href="http://www.example.com/example.php?test=test">This is a link</a>' -->
<div id="maya568mbad" >
<div id="main"><textarea name="body" id='hiitest' cols="20" rows="5" placeholder='Add Reply'></textarea><input type="submit" value="Reply" /></div></div>
<script type="text/javascript">
$(document).ready(function (e) {
$(document).on('click', '.msptd_584', function(d){
var maneno = $('#hiitest').val();
posted = 'msadhgstsmanu=' maneno;
console.log(posted);//// up to here i get correct answer
$.ajax ({
url: 'test.php',
data: posted,
type: 'POST',
processData: false,
contentType: false,
success: function(data){
console.log(data)/////The empty data returned here..!! means that the data sent to test.php is empty ;
}
});
});
});
</script>
</body>
</html>
Second page test.php
<?php
///dbcon Database PDO CONNECTION/////
$host = 'localhost';
$db = 'databasename';
$user = 'root';
$pass = '';
$port = "3306";
$charset = 'utf8mb4';
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
];
$dsn = "mysql:host=$host;dbname=$db;port=$port";
try {
$dbcon= new \PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
////dbcon Database PDO CONNECTION code Eding/////
$bodyfirst = (array_key_exists('msadhgstsmanu', $_POST) ? $_POST['msadhgstsmanu'] : "");
$email2 = 'my name';
$stmt = $dbcon->prepare("INSERT INTO test (jina,mwisho)VALUES(:nani2e,:comm_body21)");
$stmt->execute(['nani2e'=>$bodyfirst,'comm_body21'=>$email2]);
this is my database table result enter image description here
CodePudding user response:
You can apply the following changes:
index.php
$.ajax ({
url: 'http://localhost/stackOverflow/test.php',
data: {msadhgstsmanu:maneno},
type: 'POST',
success: function(data){
console.log(data)/////The empty data returned here..!! means that the data sent to test.php is empty ;
}
});
});
test.php
$bodyfirst = $_POST['msadhgstsmanu'];
I hope it helps.