Hey guys.
I have tried with autocommit, with timeout. Can not get correct value of $telefonska_str
Here is the code:
Beware - button id=“button2” has jquery function behind:
<script type="text/javascript">
$(document).ready(function() {
console.log("document is ready!!!");
$("#button2").click(function() {
var telefonska_str = document.getElementById("telefonska_str").value;
var jsonData = {};
jsonData["telefonska_str"] = telefonska_str;
console.log(telefonska_str);
// alert($(this).attr('lat'));
$.ajax({
type: "POST",
url: 'validate_tel.php',
data: {data : JSON.stringify(jsonData)},
success: function(data) {
console.log(data);
alert(data);
}
});
});
});
</script>
First page, where I get data and send it as json:
[php]
Telefonska:
Znamka:
Lokacija:
Nadaljuj
<?php
$con=mysqli_connect("localhost", "", "", "");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql=“SELECT * FROM temp ORDER by id DESC limit 1”;
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
printf ("%s (%s)\n",$row[0],$row[1]);
}
// Free result set
mysqli_free_result($result);
}
else {
echo "Telefonska stranke: " . $result[1] . “\r\n”;
}
mysqli_close($con);
?>
[/php]
Here is the validate_tel.php
[php]
<?php
header('Content-type: text/html; charset=utf-8');
/*
validate_tel.php is invoked by ajax with json data (longitude and latitude stored in data).
All return types should be echo(ed), so the javascript can read it back.
*/
// get data from ajax post method.
$jsonData = json_decode(stripslashes($_POST['data']));
// Store lat and lon separately.
$telefonska_str = $jsonData->telefonska_str;
// Prepare SQL connection.
$con = mysqli_connect("localhost","","","");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
// abort?
exit;
}
// escape variables for security
// to prevent SQL injections.
$telefonska_str = mysqli_real_escape_string($con, $telefonska_str);
// Prepare sql statement
$sql = "INSERT INTO temp (telefonska_str) VALUES ('$telefonska_str')";
// echo $sql;
// Run the query. If query fails, bail.
if (!mysqli_query($con, $sql)) {
echo "Falied to execute SQL query with statement: " . $sql;
exit;
}
?>
[/php]
How I am trying to get telefonska_str:
[php]
<?php
$con=mysqli_connect("localhost", "zzzzzzzzzz", "zzzzzzzzzz.", "zzzzzzzzz");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM temp ORDER by id DESC limit 1";
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
printf ("%s (%s)\n",$row[0],$row[1]);
}
// Free result set
mysqli_free_result($result);
}
else {
echo "Telefonska stranke: " . $result[1] . "\r\n";
}
mysqli_close($con);
?>
[/php]