I want to link my json result to the google map. I have a imei_number in the database which is the password of the user. Once the user enter the right password(imei_number in the database), the system will select the longitude and latitude of the imei_number selected and later link it to the google map's longitude and latitude.
The HTML code is:
<form method="post" action="loginServer.php">
<label> Username: </label>
<input type = "text" maxlength = "20" name = "user" id= "user" />
<br />
<label> Password: </label>
<input type = "password" maxlength= "20" name="pass" id= "pass" />
<br />
<input type ="Button" onclick="" value="Clear" />
<input type = "Submit" value = "Login"id= "btn"/>
</p>
</form>
The Google map page is
<script type = "text/javascript">
function LoadMap(){
var mapEdit = {
zoom: 8,
center: new google.maps.LatLng($.getJSON('connect.php', function(data) {
$.each(data, function(key, val) {
$("ul").append(val.latitude);
});
});
,
$.getJSON('connect.php', function(data) {
$.each(data, function(key, val) {
$("ul").append(val.longitude);
});
});
),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), mapEdit);
var marker = new google.maps.Marker({
position: new google.maps.LatLng( $.getJSON('connect.php', function(data) {
$.each(data, function(key, val) {
$("ul").append(val.latitude);
});
});
,
$.getJSON('connect.php', function(data) {
$.each(data, function(key, val) {
$("ul").append(val.longitude);
});
});
),
map: map,
});
marker.addListener('click', function(){
infowindow.open(map, marker);
});
}
</script>
</head>
<body onload = "LoadMap()">
<div id="map" style="width: 400px; height: 500px;"></div>
The connect.php is
<?php
include 'linker.php';
$sql = "SELECT * FROM android_data WHERE $pass = 'imei_number'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$data[] = $row;
$row["longitude"]);
$row["longitude"]. " - latitude: " . $row["latitude"]. "<br>";
}
} else {
echo "0 results";
}
echo json_encode($data);
mysqli_close($conn);
?>
The linker.php is
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project_gps";
$user = $_POST['user'];
$pass = $_POST['pass'];
$user = stripcslashes($user);
$pass = stripcslashes($pass);
$conn = mysqli_connect($servername, $username, $password, $dbname);
$user = mysqli_real_escape_string($conn, $user);
$pass = mysqli_real_escape_string($conn, $pass);
if($conn){
echo "connected";
}
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
But they are not all working. Please, what can I do?
Related
The value of radio button don't insert in database. I am using PHP PDO, Jquery and Ajax
$(document).ready(function() {
$('input[type="submit"]').click(function() {
var gender = $(this).val();
$.ajax({
url: "insert.php",
method: "POST",
data: {
gender: gender
},
success: function(data) {
$('#result').html(data);
}
});
});
});
<title>OnClick Insert Radio Button value into Database using PDO in Jquery Ajax PHP | SoftAOX Tutorial</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<h1> On Click Insert Radio Button Value into Database</h1>
<input type="radio" name="gender" value="Male">Male<br/><br/>
<input type="radio" name="gender" value="Female">Female<br/><br/>
<input type="radio" name="gender" value="Others">Others<br/>
<input type="submit"><br/>
<h3 id="result"></h3>
<br/>
<?php
//Insert Data
$hostname = "localhost";
$username = "root";
$password = "";
$databasename = "tut";
try {
$conn = new PDO("mysql:host=$hostname;dbname=$databasename", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST["gender"])) {
$query = "INSERT INTO gender(gender) VALUES (:gender)";
$statement = $conn->prepare($query);
$statement->execute(array(
'gender' => $_POST["gender"]
));
$count = $statement->rowCount();
if ($count > 0) {
echo "Data Inserted Successfully..!";
} else {
echo "Data Insertion Failed";
}
}
}
catch (PDOException $error) {
echo $error->getMessage();
}
?>
<script type="text/javascript">
$(document).ready(function() {
$('input[type="submit"]').click(function() {
var gender = $("[name=gender]:checked").val() || "na";
$.ajax({
url: "insert.php",
method: "POST",
data: {
gender: gender
},
success: function(data) {
$('#result').html(data);
}
});
});
});
</script>
So basically I am trying to upload a Model and Serial number into a database only on POST but SOMETIMES, not all the time, it will duplicate the entry in the database even though I only hit submit once, once submitted, the text field goes blank as it should so how is it adding it more than once?
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "database";
if ($_POST['submit'])
{
// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$mod = mysqli_real_escape_string($con,$_POST['model']);
$sn_num = mysqli_real_escape_string($con,$_POST['sn']);
$sql = "INSERT INTO rma_product (m_type,pro_sn) VALUES ('$mod','$sn_num')";
if ($con->query($sql) === TRUE) {
$success = "New record created successfully";
} else {
$success = "Failed!";
}
$con->close();
}
?>
<form method="post" name="sn_upload" id="sn_upload">
<div>
<label for="model">Model: </label><select id="model" name="model" title="Model" />
<option value="ModelA">ModelA</option>
</select><br><br>
<label for="sn">Serial Number: </label><input type="text" id="sn" name="sn" placeholder="Serial Number" pattern="[a-zA-Z0-9]{11,13}"/><br><br>
<input name="submit" type="submit" value="Submit" />
<?php if( $success) echo "<div>".$success."</div>";?>
</div>
</form>
<script>
(function() {
var input = document.getElementById('sn');
var form = document.getElementById('sn_upload');
var elem = document.createElement('div');
elem.id = 'notify';
elem.style.display = 'none';
form.appendChild(elem);
input.addEventListener('invalid', function(event){
event.preventDefault();
if ( ! event.target.validity.valid ) {
input.className = 'invalid animated shake';
elem.textContent = 'Serial Number must be between 11 and 13 characters and DO NOT include spaces.';
elem.className = 'error';
elem.style.display = 'block';
}
});
input.addEventListener('input', function(event){
if ( 'block' === elem.style.display ) {
input.className = '';
elem.style.display = 'none';
}
});
})();
</script>
How I can have image with each result? I search many websites and many tutorials but I can't understand those tutorials.
here is my index
<nav>
<div class="nav-wrapper">
<form>
<div class="input-field">
<input id="vyhledat" class="form-control input-lg" type="search" placeholder="Vyhledat film..." />
<label class="label-icon" for="search"><i class="material-icons">search</i></label>
<i class="material-icons">close</i>
</div>
</form>
</div>
</nav>
here my script
<!-- js -->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$('#vyhledat').autocomplete({
source: "php/vyhledat.php",
minLength: 3,
select: function(event, ui) {
var url = ui.item.id;
if (url != '#') {
location.href = url
}
},
open: function(event, ui) {
$(".ui-autocomplete").css("z-index", 1000)
}
})
});
</script>
And here is my php code
$keyword = trim($_REQUEST['term']); // input
$sugg_json = array();
$json_row = array();
$keyword = preg_replace('/\s+/', ' ', $keyword);
$query = 'SELECT id, nazev, href, obrazek FROM filmy WHERE nazev LIKE :term'; // query
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':term'=>"%$keyword%"));
if ( $stmt->rowCount()>0 ) {
while($recResult = $stmt->fetch(PDO::FETCH_ASSOC)) {
$json_row["id"] = $recResult['href'];
$json_row["value"] = $recResult['nazev'];
$json_row["label"] = $recResult['nazev'];
array_push($sugg_json, $json_row);
}
} else {
$json_row["id"] = "#";
$json_row["value"] = "";
$json_row["label"] = "Žádný výsledek";
array_push($sugg_json, $json_row);
}
$jsonOutput = json_encode($sugg_json, JSON_UNESCAPED_SLASHES);
print $jsonOutput;
and of course connection to my database.
$DBhost = "localhost";
$DBuser = "jirkua";
$DBpass = "koralek1";
$DBname = "jirkua";
try {
$DBcon = new PDO("mysql:host=$DBhost;dbname=$DBname",$DBuser,$DBpass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex){
die($ex->getMessage());
}`
So i already asked some questions here and fixed my problems,but i think they are growing instead of going down,lol.My problem now is i get "Undefined index" error.I will leave picture with the error here.I tried to add variables to define my fsearch.Tried things like
$fsearch = mysqli_escape_string($connect, $_POST['fsearch']);
And more simple like.
$fsearch = $_POST['fsearch'];
But it doesn't work.Here is code.
jQuery(document).ready(function ($) {
$("#food_search").keyup(function(event){
var search_term =$(this).val();
$.ajax({
type:"POST",
url:"/Food-Search",
data:{fsearch:search_term},
success:function(res){
$("#food_search_result").html(res);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!----------------------------------------------------------------
HTML
----------------------------------------------------------------->
<form method="POST">
<p>Търсене на храни: <input type="text" name="fsearch" id="food_search"></p>
</form>
<!----------------------------------------------------------------
PHP
-----------------------------------------------------------------><?php
$hostname = "localhost";
$username = "name";
$password = "password";
$databaseName = "dbName";
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
if (!empty($_POST['fsearch'])) {
$fsearch = mysqli_escape_string($connect, $_POST['fsearch']);
$req = $connect->prepare('SELECT * FROM food_data_bg WHERE title LIKE "%".$fsearch."%"');
$req->execute(array(
'fsearch'=>'%'.$_POST['fsearch'].'%'
));
if($req->rowCount()==0){
echo 'Не бяха намерени резултати!';
}
else{
while($foodsearch=$req->fetch()){
?>
<div class="search-result">
<img src="<?php echo $data['fimage']; ?>" class="fimage"/>
<span class="result-title"><?php echo $data['title'];?></span><br>
<span class="calories-total"><?php echo $data['calories total'];?></span><br>
</div>
<?php
}
}
}
?>
The code is used for searching,it must get the value of #food-search (input field) on every keyup and send it to PHP,which must check for matches in database and print it.(I think i got 1 more mistake by printing result in PHP page and returning it to java to print it in #food_search_result div)
Hope this helps.
PHP Code
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "stackoverflow";
if (!empty($_POST['fsearch'])) {
$fsearch = $_POST['fsearch'];
$mysqli = new mysqli($hostname , $username, $password, $databaseName);
if($mysqli->connect_error)
{
die("$mysqli->connect_errno: $mysqli->connect_error");
}
$query = "SELECT * FROM food_data_bg where title LIKE ?";
$stmt = $mysqli->stmt_init();
if(!$stmt->prepare($query))
{
print "Failed to prepare statement\n";
}
else
{
$searchVar = "%" . $fsearch . "%";
$stmt->bind_param("s", $searchVar);
$stmt->execute();
$result = $stmt->get_result();
if(!$result->num_rows){
echo 'Не бяха намерени резултати!';
}
while ($row = $result->fetch_array())
{
?>
<div class="search-result" style='clear:both; border:1px solid red;'>
<img src="<?php //echo $row['fimage']; ?>" class="fimage"/>
<span class="result-title"><?php echo $row['title'];?></span><br>
<span class="calories-total"><?php echo $row['calories total'];?></span><br>
</div>
<?php
}
}
$stmt->close();
$mysqli->close();
}
HTML Code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function ($) {
$("#food_search").keyup(function(event){
var search_term =$(this).val();
console.log(search_term);
$.ajax({
type:"POST",
url:"Food-Search.php",
data:{fsearch:search_term},
success:function(res){
$("#food_search_result").html(res);
console.log(res);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}
});
});
});
</script>
<!----------------------------------------------------------------
HTML
----------------------------------------------------------------->
<form method="POST">
<p>Търсене на храни: <input type="text" name="fsearch" id="food_search"></p>
</form>
<!----------------------------------------------------------------
PHP
----------------------------------------------------------------->
<div style="border:#FF0000;clear:both; padding:10px;min-height:300px;" id="food_search_result"></div>
I am trying to update mysql database table with button click. But database is not getting updated...
HTML Code :
<tr >
<td>Advt Heading :</td>
<td>
<input type="hidden" name="idnew" id="idnew" value="<?=$member_data['id']?>"> //retrieved from mysql database
<input type="text" name="advt_headingnew" id="advt_headingnew" value="<?=stripslashes($member_data['advt_heading']);?>" /> //retrieved from mysql database ...**I want to edit its previus retreived value...and update database**
<input name="submit" type="button" id="submit" value="Update" />
</td>
</tr>
SCRIPT :
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#submit').click(function(){
var advt_headingnew = $("#advt_headingnew").val();
var idnew = $("#idnew").val();
$.ajax({
type:'POST',
url:'update-advt-heading.php',
data: "advt_headingnew="+advt_headingnew+"&idnew="+idnew,
success:function( msg ) {
alert( "Data Saved: " + msg );
}
});
});
});
</script>
PHP - update-advt-heading.php CODE :
<?
$user_name = "databaseusername";
$password = "databasepassword";
$database = "databasename";
$server = "localhost";
mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database);
$heading = $_POST['advt_headingnew'];
$id=$_POST["idnew"];
if (isset($_POST['submit'])){
$queryStr = "UPDATE tablename SET advt_heading='$heading' WHERE id='$id'";
if ( mysql_query($qyeryStr)){
return "success!";
}else{
return "failed!";
}
}
?>
Change your js code to this
$(document).ready(function () {
$('#submit').click(function(){
var advt_headingnew = $("#advt_headingnew").val();
var idnew = $("#idnew").val();
var submitval = $(this).val();
$.ajax({
type:'POST',
url:'update-advt-heading.php',
data: "advt_headingnew="+advt_headingnew+"&idnew="+idnew+"&submit="+submitval,
success:function( msg ) {
alert( "Data Saved: " + msg );
}
});
});
});
I have changed
var advt_headingnew = $("#advt_headingnew").val();
var idnew = $("#idnew").val();
To
var advt_headingnew = document.getElementsByName("advt_headingnew")[0].value;
var idnew = document.getElementsByName("idnew")[0].value;
NOW IT IS WORKING..