Skip to content Skip to sidebar Skip to footer

Pulling The String Value In Javascript

So I have this unordered list of items that is made from querying a mysql database
  • apple
  • orange
  • pear&

Solution 1:

Use JQUERY AJAX for send data into php page

/* Get value from clicked li */
	$(function() {

		let value = "";

		$('ul li').on("click", function() {

			value = $(this).text();
			console.log(value);
		});

		/* AJAX for send value into result.php page */

		$.post("result.php", {value: value}, function(res) {

			console.log(res); // return from result.php page 
		}); 

	});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><ul><li>apple</li><li>orange</li><li>pear</li></ul>

result.php

if(isset($_POST['value']) && !empty($_POST['value'])) {

        echo$_POST['value'];
    }

Solution 2:

This is how you can do it. On click you can change the page or do whatever you want. In the new url you can append the fruit value as a parameter to pass it to php

<ul><lionclick="fruitsClick('apple')">apple</li><lionclick="fruitsClick('orange')">orange</li><lionclick="fruitsClick('pear')">pear</li></ul><script>functionfruitsClick(fruit){
       // do whatever you want  window.location.href = "[yourpageurl]?fruit=" + fruit
}
</script>

Post a Comment for "Pulling The String Value In Javascript"