admin管理员组文章数量:1387401
I'm quite new to AJAX. I did some of them with javascript that returns HTML, but I'm struggling with something that I think should be really simple.
I have a header with an inline element:
<header>
<span class="right_alignment">
<a id="delete_work" href="">
<input id ="work_id" type="hidden" value="<?php echo $work_id ?>"/>
<i class="fa fa-plus" aria-hidden="true">
</i>
</a>
</span>
</header>
I want to send $work_id to a PHP script that executes a MySQL query that deletes this record in the database. It has to be a POST request, and as it is an inline element I can't use a form. So AJAX is the way. This is my try:
$( document ).ready(function() {
$("#delete_work").click(function () {
$.ajax({
url: 'scripts/script-delete-work.php',
type: 'POST',
data: $('#work_id').val(),
});
});
});
For now script-delete-work.php don't worry me, so is as simple as:
$edition_id = $_POST['work_id'];
echo $work_id;
But this setup doesn't work. I know that it is the simplest form of an AJAX, but I can't figure how to send the data or what is failing. Any help?
Thanks!
EDIT -- Ok, I solved the data thing. What I have now is:
HTML
<span class="span_edit_right">
<a id="delete_work" href="">
<input id="work_id" type="hidden" value="<?php echo $work_id; ?>"/>
<i class="fa fa-trash-o" aria-hidden="true">
</i>
</a>
</span>
Script:
$( document ).ready(function() {
$("#delete_work").click(function () {
var val = $('#work_id').val();
$.ajax({
url: 'scripts/script-delete-work.php',
type: 'POST',
data: { "work_id" : val },
success: function ()
{
alert("works!");
}
});
});
});
PHP
<?php echo $_POST['work_id'];
When I click on #delete_work
it returns the alert "works"
, and reload the page, but doesn't show the page with <?php echo $_POST['work_id'];
.
I'm quite new to AJAX. I did some of them with javascript that returns HTML, but I'm struggling with something that I think should be really simple.
I have a header with an inline element:
<header>
<span class="right_alignment">
<a id="delete_work" href="">
<input id ="work_id" type="hidden" value="<?php echo $work_id ?>"/>
<i class="fa fa-plus" aria-hidden="true">
</i>
</a>
</span>
</header>
I want to send $work_id to a PHP script that executes a MySQL query that deletes this record in the database. It has to be a POST request, and as it is an inline element I can't use a form. So AJAX is the way. This is my try:
$( document ).ready(function() {
$("#delete_work").click(function () {
$.ajax({
url: 'scripts/script-delete-work.php',
type: 'POST',
data: $('#work_id').val(),
});
});
});
For now script-delete-work.php don't worry me, so is as simple as:
$edition_id = $_POST['work_id'];
echo $work_id;
But this setup doesn't work. I know that it is the simplest form of an AJAX, but I can't figure how to send the data or what is failing. Any help?
Thanks!
EDIT -- Ok, I solved the data thing. What I have now is:
HTML
<span class="span_edit_right">
<a id="delete_work" href="">
<input id="work_id" type="hidden" value="<?php echo $work_id; ?>"/>
<i class="fa fa-trash-o" aria-hidden="true">
</i>
</a>
</span>
Script:
$( document ).ready(function() {
$("#delete_work").click(function () {
var val = $('#work_id').val();
$.ajax({
url: 'scripts/script-delete-work.php',
type: 'POST',
data: { "work_id" : val },
success: function ()
{
alert("works!");
}
});
});
});
PHP
<?php echo $_POST['work_id'];
When I click on #delete_work
it returns the alert "works"
, and reload the page, but doesn't show the page with <?php echo $_POST['work_id'];
.
- Check the answer below please. Might be helpful. – Kamran Jabbar Commented Jul 29, 2017 at 11:49
-
Try
data: {work_id: $('#work_id').val()}
and get$_POST['work_id']
in php script. – Ivan Bolnikh Commented Jul 29, 2017 at 11:49 -
add tihs
alert(val);
aftervar val = $('#work_id').val();
, findout variable has value or not. – Asif Thebepotra Commented Jul 29, 2017 at 12:51
5 Answers
Reset to default 2Use this code please
$( document ).ready(function() {
$("#delete_work").click(function () {
var val = $('#work_id').val();
$.ajax({
url: 'scripts/script-delete-work.php',
type: 'POST',
data: { "work_id" : val },
success: function ()
{
}
});
});
});
And on your server side just
echo $_POST['work_id'];
Solved!
The problem is apparently that with AJAX you are not going to be sent to another PHP page, so I couldn't see the echo $_POST['work_id'];
. But now it works, it was the data problem…
Actual code is:
<span class="span_edit_right">
<a id="delete_work" href="">
<input id="work_id" type="hidden" value="<?php echo $work_id; ?>"/>
<i class="fa fa-trash-o" aria-hidden="true">
</i>
</a>
</span>
Script:
$( document ).ready(function() {
$("#delete_work").click(function () {
var val = $('#work_id').val();
$.ajax({
url: 'scripts/script-delete-work.php',
type: 'POST',
data: { "work_id" : val },
success: function ()
{
alert("works!");
}
});
});
});
Thanks!
use the ajax success function. and return $edition_id instead of work_id
success: function(work_id)
It's just that you don't get the value of the checked box.
You forgot ;
in value="<?php echo $work_id; ?>"
.
You are not sending data correctly. You should pass data in javascript object form i.e {'parameter_name':parameter_value} so below is the code.
$( document ).ready(function() {
$("#delete_work").click(function () {
var wrkId = $('#work_id').val();
$.ajax({
url: 'scripts/script-delete-work.php',
type: 'POST',
data: {'work_id':wrkId},
});
});
});
Or
$( document ).ready(function() {
$("#delete_work").click(function () {
var wrkId = $('#work_id').val();
$.post('scripts/script-delete-work.php',{'work_id':wrkId},function(response){
console.log(response);
});
});
});
本文标签: javascriptRun php script with AJAX and POSTStack Overflow
版权声明:本文标题:javascript - Run php script with AJAX and POST - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744485091a2608403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论