更改值的HTML表和外部Php文件。

时间:2023-01-26 14:00:51

I have table like this:

我有这样的桌子:

<table border="1">
	<tr align="left" valign="top">
		<th>ID</th>
		<th>NAME</th>
		<th>VALUE</th>
	</tr>
	<tr align="left" valign="top">
		<td>1</td>
		<td>Item 1</td>
		<td><button>-</button> 5 <button>+</button></td>
	</tr>
</table>

It is generated by Php and all information are from MySql DB.. I would like to activate buttons "-" and "+", so when I click minus, value 5 (in this case) will became 4 or when I click plus value 5 will became 6, and also in MySql DB some external php file will change values in real time..

它是由Php生成的,所有的信息都来自MySql DB。我想要激活按钮“-”和“+”,所以当我点击-时,值5(在本例中)会变成4,或者当我点击+值5会变成6,同时在MySql DB中一些外部php文件会实时改变值。

I tried with some AJAX scripts, but I am stuck.. Please help.. Thanks..

我尝试了一些AJAX脚本,但是我被卡住了。请帮助. .谢谢. .

4 个解决方案

#1


1  

Try storing first the value in an HTML element like <span></span>, and assign class tag for the + and - button:

尝试先将值存储在这样的HTML元素中,并为+和-按钮分配类标签:

<table border="1">
  <tr align="left" valign="top">
    <th>ID</th>
    <th>NAME</th>
    <th>VALUE</th>
  </tr>
  <tr align="left" valign="top">
    <td>1</td>
    <td>Item 1</td>
    <td><button class="minus">-</button> <span>5</span> <button class="plus">+</button></td>
  </tr>
</table>

Then create the script:

然后创建脚本:

$(document).ready(function(){

  $(document).on("click", ".minus", function(){ /* WHEN MINUS IS CLICKED */

    var elem = $(this); /* THE ELEMENT CLICKED */
    var id = elem.parent().siblings(":first").text(); /* ID OF THIS ROW IN THE DATABASE */
    var current_no = Number(elem.closest("td").find("span").html()); /* CURRENT VALUE OF THIS ROW */
    var new_no = current_no - 1; /* REDUCE ONE VALUE */
    elem.closest("td").find("span").html(new_no); /* REPLACE WITH THE NEW NUMBER INSIDE THE SPAN */
  });

  $(document).on("click", ".plus", function(){ /* WHEN PLUS IS CLICKED */
    var elem = $(this); /* THE ELEMENT CLICKED */
    var id = elem.parent().siblings(":first").text(); /* ID OF THIS ROW IN THE DATABASE */
    var current_no = Number(elem.closest("td").find("span").html()); /* CURRENT VALUE OF THIS ROW */
    var new_value = current_no + 1; /* ADD ONE VALUE */
    elem.closest("td").find("span").html(new_no); /* REPLACE WITH THE NEW NUMBER INSIDE THE SPAN */
  });

});

For updating the data in your MySQL database in real-time, you need to use AJAX.

要实时更新MySQL数据库中的数据,需要使用AJAX。

$.ajax({ /* START AJAX */
  type: "POST", /* METHOD TO USE TO PASS THE DATA */
  url: "update.php", /* FILE DESTINATION OF THE DATA */
  data: {"id": id, "value": new_value} /* THE DATA TO BE PASSED ON */
}); /* END OF AJAX */

On the update.php file, it must contain the UPDATE query:

在更新。php文件,必须包含更新查询:

/*** YOUR ESTABLISHED CONNECTION HERE ***/
$stmt = $connection->prepare("UPDATE table SET value = ? WHERE id = ?");
$stmt->bind_param("ii", $_POST["value"], $_POST["id"]);
$stmt->execute();

Here is a jsfiddle (but without the AJAX).

这里有一个jsfiddle(但是没有AJAX)。

#2


1  

If you use a form to fetch this value, you can use the <input type="number" />:

如果使用表单获取该值,可以使用:

<table border="1">
	<tr align="left" valign="top">
		<th>ID</th>
		<th>NAME</th>
		<th>VALUE</th>
	</tr>
	<tr align="left" valign="top">
		<td>1</td>
		<td>Item 1</td>
		<td><input type="number" value="5" /></td>
	</tr>
</table>

Modern web browsers will display buttons to decrease or increase the value.

现代web浏览器将显示按钮以减少或增加价值。

#3


1  

Using JS/jQuery:

使用JS / jQuery:

/* JS: */

var counter = 5;

$(".down").on("click", function() {
  if (counter > 0) {
    counter--;
    $('.anumber').text(counter);
  }
});

$(".up").on("click", function() {
  counter++;
  $('.anumber').text(counter);
});
<!-- HTML: -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <tr align="left" valign="top">
    <th>ID</th>
    <th>NAME</th>
    <th>VALUE</th>
  </tr>
  <tr align="left" valign="top">
    <td>1</td>
    <td>Item 1</td>
    <td>
      <button class="down">-</button> <span class="anumber">5</span> 
      <button class="up">+</button>
    </td>
  </tr>
</table>

JSFiddle

JSFiddle

#4


1  

Better if you can change your html and add some classes, check the following suggestion :

如果您可以更改html并添加一些类,请查看以下建议:

$(function(){
    $('body').on('click' ,'.minus', function(){
        var current_number = parseInt($(this).parent().find('.number').text());
        current_number--;
        $(this).parent().find('.number').text(current_number);
    });

    $('body').on('click' ,'.plus', function(){
        var current_number = parseInt($(this).parent().find('.number').text());
        current_number++;
        $(this).parent().find('.number').text(current_number);
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <tr align="left" valign="top">
    <th>ID</th>
    <th>NAME</th>
    <th>VALUE</th>
  </tr>
  <tr align="left" valign="top">
    <td>1</td>
    <td>Item 1</td>
    <td>
      <button class='minus'>-</button>
      <span class='number'>5</span>
      <button class='plus'>+</button>
    </td>
  </tr>
</table>

Hope this helps.

希望这个有帮助。

#1


1  

Try storing first the value in an HTML element like <span></span>, and assign class tag for the + and - button:

尝试先将值存储在这样的HTML元素中,并为+和-按钮分配类标签:

<table border="1">
  <tr align="left" valign="top">
    <th>ID</th>
    <th>NAME</th>
    <th>VALUE</th>
  </tr>
  <tr align="left" valign="top">
    <td>1</td>
    <td>Item 1</td>
    <td><button class="minus">-</button> <span>5</span> <button class="plus">+</button></td>
  </tr>
</table>

Then create the script:

然后创建脚本:

$(document).ready(function(){

  $(document).on("click", ".minus", function(){ /* WHEN MINUS IS CLICKED */

    var elem = $(this); /* THE ELEMENT CLICKED */
    var id = elem.parent().siblings(":first").text(); /* ID OF THIS ROW IN THE DATABASE */
    var current_no = Number(elem.closest("td").find("span").html()); /* CURRENT VALUE OF THIS ROW */
    var new_no = current_no - 1; /* REDUCE ONE VALUE */
    elem.closest("td").find("span").html(new_no); /* REPLACE WITH THE NEW NUMBER INSIDE THE SPAN */
  });

  $(document).on("click", ".plus", function(){ /* WHEN PLUS IS CLICKED */
    var elem = $(this); /* THE ELEMENT CLICKED */
    var id = elem.parent().siblings(":first").text(); /* ID OF THIS ROW IN THE DATABASE */
    var current_no = Number(elem.closest("td").find("span").html()); /* CURRENT VALUE OF THIS ROW */
    var new_value = current_no + 1; /* ADD ONE VALUE */
    elem.closest("td").find("span").html(new_no); /* REPLACE WITH THE NEW NUMBER INSIDE THE SPAN */
  });

});

For updating the data in your MySQL database in real-time, you need to use AJAX.

要实时更新MySQL数据库中的数据,需要使用AJAX。

$.ajax({ /* START AJAX */
  type: "POST", /* METHOD TO USE TO PASS THE DATA */
  url: "update.php", /* FILE DESTINATION OF THE DATA */
  data: {"id": id, "value": new_value} /* THE DATA TO BE PASSED ON */
}); /* END OF AJAX */

On the update.php file, it must contain the UPDATE query:

在更新。php文件,必须包含更新查询:

/*** YOUR ESTABLISHED CONNECTION HERE ***/
$stmt = $connection->prepare("UPDATE table SET value = ? WHERE id = ?");
$stmt->bind_param("ii", $_POST["value"], $_POST["id"]);
$stmt->execute();

Here is a jsfiddle (but without the AJAX).

这里有一个jsfiddle(但是没有AJAX)。

#2


1  

If you use a form to fetch this value, you can use the <input type="number" />:

如果使用表单获取该值,可以使用:

<table border="1">
	<tr align="left" valign="top">
		<th>ID</th>
		<th>NAME</th>
		<th>VALUE</th>
	</tr>
	<tr align="left" valign="top">
		<td>1</td>
		<td>Item 1</td>
		<td><input type="number" value="5" /></td>
	</tr>
</table>

Modern web browsers will display buttons to decrease or increase the value.

现代web浏览器将显示按钮以减少或增加价值。

#3


1  

Using JS/jQuery:

使用JS / jQuery:

/* JS: */

var counter = 5;

$(".down").on("click", function() {
  if (counter > 0) {
    counter--;
    $('.anumber').text(counter);
  }
});

$(".up").on("click", function() {
  counter++;
  $('.anumber').text(counter);
});
<!-- HTML: -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <tr align="left" valign="top">
    <th>ID</th>
    <th>NAME</th>
    <th>VALUE</th>
  </tr>
  <tr align="left" valign="top">
    <td>1</td>
    <td>Item 1</td>
    <td>
      <button class="down">-</button> <span class="anumber">5</span> 
      <button class="up">+</button>
    </td>
  </tr>
</table>

JSFiddle

JSFiddle

#4


1  

Better if you can change your html and add some classes, check the following suggestion :

如果您可以更改html并添加一些类,请查看以下建议:

$(function(){
    $('body').on('click' ,'.minus', function(){
        var current_number = parseInt($(this).parent().find('.number').text());
        current_number--;
        $(this).parent().find('.number').text(current_number);
    });

    $('body').on('click' ,'.plus', function(){
        var current_number = parseInt($(this).parent().find('.number').text());
        current_number++;
        $(this).parent().find('.number').text(current_number);
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <tr align="left" valign="top">
    <th>ID</th>
    <th>NAME</th>
    <th>VALUE</th>
  </tr>
  <tr align="left" valign="top">
    <td>1</td>
    <td>Item 1</td>
    <td>
      <button class='minus'>-</button>
      <span class='number'>5</span>
      <button class='plus'>+</button>
    </td>
  </tr>
</table>

Hope this helps.

希望这个有帮助。