What should be happening
- I'm having problems with passing strings using ajax. What my code should do, is to grab the text from fields and pass it to "edit.php" where a query will update the database with recieved data.
What's happening
- Data from the field is being recieved and then instantly sent to "edit.php", where a query updates the database with recieved values.
Where's the problem
- While not having issues with sending numbers, all data in the field is integer, ajax(I guess) doesn't send data if it contains at least 1 character different from a number.
Example of my problem
- THIS IS WITH NUMBERS
field1 = 123
field2 = 321
query : SUCCESS
- THIS IS WHERE PROBLEMS OCCURE
field1 = 123a
field2 = 321a
query : FAIL
Ajax Code
<script text="text/javascript">
$('#button_save').click(function(){
var edited_message_id = document.getElementById('id_message_hidden').value;
var edited_title = document.getElementById('user_event_title').value;
var edited_message = document.getElementById('user_message_input').value;
alert(edited_title.concat(edited_message));
if(confirm("Deseja editar esta mensagem?")){
$.ajax({
url: 'edit.php',
type: 'post',
data: {idmensagem:edited_message_id,
newtitle:edited_title,
newmessage:edited_message,}
});
};
return false;
});
</script>
edit.php
<?php
session_start();
include 'connect.php';
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(!isset($_SESSION['pass'])){ //if login in session is not set
header("Location:index.php");
}
$message_id = $_POST['idmensagem'];
$new_title = $_POST['newtitle'];
$new_message = $_POST['newmessage'];
$query_update = mysqli_query($con, "UPDATE tbl_mensagens SET title_text = $new_title, txt_mensagem = $new_message WHERE id_mensagem = $message_id");
?>