How to isset($_POST) with a click on either div or a href?

Hi, I am very much aware about the submit process of form element and used many times isset($_POST[‘…’]) to check it.
Now, I have php form that contains 5 divs and a href tag, for one Name and i the same way I have N names as per database.
I want that when some one click on the Name div the corresponding pages with name as name.php should open and display the corresponding values on the page as per fetched name.

I knw that how to implement it with form submit as:

read the first page value via POST method and open change the location to the name.php page with read string and use Get method to read the global variable in name.php page and display the corresponding values on the name.php page …

But, how to achieve it with tag a href or div click

or

How to convert the N names block to button or input type submit??? I think this is yuk …

Use e.g. jQuery to set an onclick event on the DIV.

Great…

But, is it possible to generate POST request on the same page with clicked variable name…

BTW the problem has resolved by changing a href to input type with image

You can send AJAX (POST) request with jquery, too.

I don’t fully understand what you are trying to accomplish but this shows how you can use jQuery to POST after a click on a div using data stored within the div itself.

HTML:

<div class="post-name" data-name="John Smith"></div>
<div class="post-name" data-name="Jane Doe"></div>

jQuery:

$('div.post-name').on('click', function() {
    var ajax = $.ajax({
        type: 'POST',
        url: 'page.php',
        data: { name: $(this).data('name') }
    });
    // Process success/failure here via ajax variable (http://api.jquery.com/jquery.ajax/)
});

Hi, Many Thanks for various methods…
with Jquery Ajax post request, the problem cannot be resolved because this method send request to page.php and will get something in return and I will be on the same way in this way…
as I dont want to come back… means…
When I click on image or on a div I want to read the name or id of clicked image or div and generate the URL and change the location to another php page. with the passed variable and reterive it on another page by GET method.

I solved it by:

form autocomplete="off" method="post" class="no-gutter-2" id="frmindex">';
                            echo '<div class="col-md-4 col-sm-6 mt-4">';
				echo '<div class="index-package-container">';
                                    
					//echo '<a href="#" id='."$city_name".'>';
                                         echo '<div class="index-package-image border-three">';
						echo '<input type="image"  src='."$imgpath".'  name="destination" value='."$city_name".' class="img-responsive">';
						echo '</div>';
							echo '<h1>'.$city_name.'</h1>';
                                                        echo '<h2>'.sprintf("%02d", $total).' Packages</h2>';
                                                        echo '<h3>Starting from &#8377; ' .number_format($price).'</h3>';
						//echo '</a>';
                                                
					echo '</div>';
				echo '</div>';
                       echo '</form>';
include "script-php/common.php";
    if(isset($_POST["destination"])){
        $retvalue=$_POST["destination"];
         $_SESSION["package_string"] = $retvalue;
         var_dump($retvalue);
        header("location:../xyz/destinations/".$retvalue."");
    }

My problem is resolved by the above method but the thing is that its not displaying on server as you can see on this link

Joys4ever

But its working on local xamp server with all features and functionality

I’m surprised that works with no action parameter in the form, or is there some JS somewhere that grabs the click? And my dev console says you’ve got two elements with the same id, which also might not help.

ETA - if I run that link from IE, clicking on either of the destinations just re-draws the initial page. If I do the same from Chrome, clicking on a destination opens the list of packages for that destination.

1 Like

There doesn’t need to be an action attribute. If you are trying to target itself, then using no action attribute apparently “works”. Though this is invalid HTML since the validator will complain about it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.