How to reference an ID to delete [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I´m trying to remove a div with jquery but I don´t know how can I refer to the ID because it´s a PHP var: id="delete$count"
My Javascript code:
<script type="text/javascript">
function deleteFile(fname,directory)
{
$.ajax({ url: "delete_img.php",
data: {"file":fname,"directory":directory},
type: 'post',
success: function(output) {
alert(output);
$('div').remove();
}
});
}
</script>
My PHP code:
$count=1;
echo '<div class="gallery-item" id="delete$count"></div>';
echo "<div><a class='ico ico-delete' rel='9' rev='2' href='#'onclick='deleteFile(\"$file\",\"$directory\")'>
</a></div>";
$count++;

I think the below will send delete$count verbatim to the response stream without evaluating $count
echo '<div class="gallery-item" id="delete$count"></div>';
Change it to
echo "<div class='gallery-item' id='delete$count'></div>";
so as for PHP to interpolate $count.
Try then passing this as a third parameter to your function as
echo "<div><a class='ico ico-delete' rel='9' rev='2' href='#'onclick='deleteFile(\"$file\",\"$directory\",\"delete$count\")'>
and collect its value in a third argument to
function deleteFile(fname,directory, deletecount) { }
and replace
$('div').remove();
by $('#' + deletecount).remove();

If i understund correctly, you want to achieve the following:
use AJAX to tell server to delete file
remove same file from current view without reloading page
If you read file information from database, i would suggest you to use row-id or something similar as 'gallery-item' ids, and as argument for deleteFile() function.
In case you dont, you can use paired value of $directory and $file instead.
echo '<div class="gallery-item" id="'. $directory . $file . '" ></div>';
echo "<div><a class='ico ico-delete' rel='9' rev='2' href='#'onclick='deleteFile(\"$file\",\"$directory\")'>
</a></div>";
With this, you can replace your $('div').remove() with$('#'+directory+fname).remove()

Related

Display that User Current Location as the value of the Textbox [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have here a codes that displays the current location of the user, this codes is already working. My problem is how to put the id "#location" as the value for my textbox in html form so that the current location will put in a textbox form.
$(document).ready(function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showLocation);
} else {
$('#location').html('Geolocation is not supported by this browser.');
}
});
function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$.ajax({
type:'POST',
url:'../geo/getLocation.php',
data:'latitude='+latitude+'&longitude='+longitude,
success:function(msg){
if(msg){
$("#location").html(msg);
}else{
$("#location").html('Not Available');
}
}
});
}
HTML:
Location: <input id="location" type="text" value="" size="50">
<br>
If the ajax request works correctly (we don't know what kind of data it returns), when the problem is in wrong choice of method.
Use .val() instead of .html() because you can't insert html inside input.
if (msg) {
$("#location").val(msg);
} else {
$("#location").val('Not Available');
}

Accessing multiple PHP pages through array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need some help with this PHP pages.
I don't understand
session_start();
require("config/db.php");
if(isset($_GET['page'])){
$pages=array("menu","cart");
if(in_array($_GET['page'], $pages)) {
$_page=$_GET['page'];
}else{
$_page="menu";
}
}else{
$_page="menu";
}
?>
My code at the bottom calls $_page and it will display that PHP page,
in this case, menu.php. How do I make it call menu2.php if I were to select an option that changes the value to menu2?
EDIT:
Thanks for all your comments! I would like to add on that this is just a container in the website, and my output will be derived from
Please look at the sample code i created below, this should give you an idea, I hope.
<section href="#" id="showItem"></section>
<select id="options">
<option value="page1.php">Page 1</option>
<option value="page2.php">Page 2</option>
</select>
<script type="text/javascript" src="js/jquery-2.2.0.min.js"></script>
<script type="text/javascript">
var options = $('select#options').val();
var section = $('section#showItem');
var getPage = function (url) {
$.get(url).success(function (data) {
section.html(data);
}).error(function () {
section.html("error loading page");
});
};
$('select#options').change(function () {
var options = $('select#options').val();
getPage(options);
});
$(document).ready(function () {
getPage(options);
});
</script>
Use
header("Location:" . $_page . ".php");
where location can be any valid url/page name on the server relative to the current document.
just try: header('Location:'.$_page.'.php'); it will redirect to selected page

How to send an id broughted by json from ajax to a php post [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this ajax to show a html table, the thing is, i have a method on viewmap.php to show the ubication depending the id you clicked at the table, but i need to send the uid to viewmap.php so the php can search it at the database, and i don't extactly know how to do that
$.ajax({
type: "GET",
url: "editinplace.php?tabla=1"
})
//Vector
.done(function(json)
{
json = $.parseJSON(json)
for(var i=0;i<json.length;i++)
{
$('.editinplace').append
(
"<tr><td class='id' value='uid'>"
+json[i].uid
+"</td><td>"
+json[i].name
+"</td><td>"
+json[i].ape
+"</td><td>"
+json[i].phone
+"</span></td><td class='editable' data-campo='status'><span>"
+json[i].status
+"</span><td>"
+"<a href='viewmap.php?uid=$uid'>View on map</a>"
+"</td>"
);
}
//
});
Is it not the uid you have from your json array?
"<a href='viewmap.php?uid="+json[i].uid+"'>View on map</a>"
If you want to pull it from php, you'd echo it like this
$.ajax({
type: "GET",
url: "editinplace.php?tabla=1"
})
//Vector
.done(function(json)
{
json = $.parseJSON(json)
for(var i=0;i<json.length;i++)
{
var uid = "<?php echo $uid; ?>";
$('.editinplace').append
(
"<tr><td class='id' value='uid'>"
+json[i].uid
+"</td><td>"
+json[i].name
+"</td><td>"
+json[i].ape
+"</td><td>"
+json[i].phone
+"</span></td><td class='editable' data-campo='status'><span>"
+json[i].status
+"</span><td>"
+"<a href='viewmap.php?uid="+uid+"'>View on map</a>"
+"</td>"
);
}
//
});
If you have shorthand PHP echo tags turned on, its simply -
"<a href='viewmap.php?uid=<?= $uid ?>'>View on map</a>"#
Otherwise its like this with full tags -
"<a href='viewmap.php?uid=<?php echo($uid); ?>'>View on map</a>"#

php variable changes when clicked on html link [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make a game in HTML and PHP and I'm pretty new to coding. The point is I need to execute a PHP script by clicking an HTML element. Is this possible?
My code would look something like this
$money = 100;
and by clicking on:
<a>rob a shop</a>
it must excecute:
function rob_shop(){
$money += 75;
echo "You now have $money $";}
Try this, is PHP:
<?php
if (isset($_GET['money'])) {
$money = rob_shop($_GET['money']);
echo 'You now have: '.$money.'<br>';
} else {
$money = 100;
echo 'You now have: '.$money.'<br>';
}
echo 'rob a shop';
function rob_shop($money){
$money = $money + 75;
return $money;
}
?>
But the best way to do it is with ajax to avoid refreshing the page.
Add extra param to link and check with php and call function.
rob money
Then check for link
if (isset($_GET['fun']))
{
runFun();
}
you will need to use javascript and an ajax call or you could just do it in javascript.
<a id="shop">rob a shop</a>
<div id="response"></div>
// include jquery
<script>
$( "#shop" ).click(function() {
var money = money + 75;
$("#response").html( "YOU NOW HAVE $"+money );
// or make an ajax call to a php script and return the value
});
</script>

Return value for php class not returning correctly [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am attempting to return a value from a php class from within Javascript:
<html>
<?php
function tester()
{
return "testing";
}
?>
<script type="text/javascript">
var val= "<?php tester(); ?>";
document.write(val);
document.write('Finished!');
</script>
</html>
Nothing is being returned, rather I simply get a blank screen (apart from the "Finished" message) =(
That is happening because you are just returning the value, to output you have to echo it:
<?php echo tester(); ?>
You're not doing anything with the output of tester(). Try this instead:
var val= <?php echo json_encode(tester()); ?>;
You need to echo it.
Either use:
var val= "<?php echo tester(); ?>"
Or if you have short tags enabled:
var val= "<?=tester()?>"

Categories