0

So I have this unordered list of items that is made from querying a mysql database

    <ul>
    <li>apple</li>
    <li>orange</li>
    <li>pear</li>
    </ul>

I want there to be an onclick event that will pass 'apple' when I click apple and 'orange' when I click orange.

I also want to pass this information to another page through php. So this is my idea for the javascript function.

<script>
function passName(obj){
var pass = "<?php $x= " + obj.getName() + " ?>";
}

function getname(obj){
return 'string';
}
</script>

My Question: is there a method that exists within JavaScript that allows me to pull the raw string value of my unorderedlist without writing my own function? If I have to write my own JavaScript function, is there a way to set the 'name' strings automatically, while the list is populating?

1
  • 1
    "allows me to pull the raw string value of my unorderedlist" get the innerText of the <ul>. "set the 'name' strings automatically, while the list is populating" . What strings are you referring to? var pass = "<?php $x= " + obj.getName() + " ?>"; will never work - you fundamentally mis-understand how the web works. That code will execute before your page loads, which is when PHP was executing. Then your HTTP request ended and it stopped. If you want PHP to execute again you have to make another request to the server - either through a postback or an ajax request, and send your data in it Commented Jan 8, 2018 at 5:57

2 Answers 2

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 
		}); 

	});
<script src="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'];
    }
Sign up to request clarification or add additional context in comments.

2 Comments

this assumes OP is using jquery. That's fine I guess but you should make it clear since the question doesn't mention it
HI, thanks for you reply. I would like to understand this code more; I'm a bit new to jquery and javascript. I understand your calling the Ajax library from the url, but I want to understand what is going on after the 'let value' command
1

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>
       <li onclick="fruitsClick('apple')">apple</li>
       <li onclick="fruitsClick('orange')">orange</li>
       <li onclick="fruitsClick('pear')">pear</li>
    </ul>

<script>
function fruitsClick(fruit){
       // do whatever you want  
       window.location.href = "[yourpageurl]?fruit=" + fruit
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.