javascript posting data to php file
Get the solution ↓↓↓Im trying to insert a base64 image string into my database, Iv'e been messing around with this for a few hours now, tried various different methods and for some reason I can't seem to get it to post the data to my php script.
app.js
saveButton.addEventListener("click", function (event) {
if (signaturePad.isEmpty()) {
alert("Please provide signature first.");
} else {
var basevalue = signaturePad.toDataURL();
$.ajax({
type: "POST",
url: "insert.php",
data: { basevle: basevalue }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
}
});
insert.php
basevalue = $_POST['basevle'];
try {
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("INSERT INTO signatures VALUES (0,:sigbase)");
$stmt->bindParam(':sigbase', $signature);
$signature = $basevalue);
$stmt->execute();
echo "added";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
I know there's nothing wrong with the php file as Iv'e tested it and it's inserting values, it seems to be the app.js not sending the post data
Anyone got any thoughts on what could be causing the issue ?
Answer
Solution:
It's impossible that this PHP file is working.
First of all, you must declare yourbaseValue
variable with a$
prefixing it.
Second, you're using a$signature
variable before even declaring it. You're declaring it and passing the$baseValue
value to it after using it, what is never gonna work.
You must put the line$signature = $baseValue;
before calling thebindParam
method.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: constant expression contains invalid operations
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.