Jump to content

Check which button was clicked in HTML form using PHP....help.


Bl4ckMaj1k

Recommended Posts

This is most likely a simple issue. I thought it would be a piece of cake for me but boy was I wrong.

 

What I want to do is capture which button on my form was pressed. With that information I wish to send the form to a javascript popup function (using the onclick command). The new page that pops up will have the following type of link:

 

new_page.php?page_id=[whatever button was clicked]

 

I can't seem to get this to work. The popup works fine but my button name won't show up in the link. Below I have added the code I am using. Let me know what may be the issue here. Any help will be greatly appreciated.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="js/jquery-1.5.js" type="text/javascript"></script>

<script type="text/javascript">
<!--
function deny_approval() {
window.open( "deny_approval.php?section=<?php echo $_POST['deny_profile_approval']; ?>", "myWindow", 
"status = 1, toolbar = no, scrollbars = yes, location = no, resizable = no, height = 600, width = 600, resizable = 0" )
}
//-->
</script>

</head>

<body>
<form action="" method="post" enctype="multipart/form-data">

<input name="deny_profile_approval1" type="button" onclick="deny_approval()" value="Deny Contact Info1" /><br /><br />
<input name="deny_profile_approval2" type="button" onclick="deny_approval()" value="Deny Contact Info2" /><br /><br />
<input name="deny_profile_approval3" type="button" onclick="deny_approval()" value="Deny Contact Info3" /><br /><br />
<input name="deny_profile_approval4" type="button" onclick="deny_approval()" value="Deny Contact Info4" /><br /><br />

</form>

 

I don't know what I am doing wrong. I would think the $_POST should read the value I have for the input??

 

I started as a complete novice but now I think I am a bit more advanced than I used to be. Even still, small issues like this never cease to puzzle me. I look forward to your feedback.

Link to comment
Share on other sites

You need to set the name attribute of your inputs to all be exactly the same, deny_profile_approval.

 

Tried that and unfortunately still didn't work.

 

You would need to use JS to tack the value on to the URL. The php variable won't have a value until the form is submitted and processed server-side, so all you'll have is an empty section= unless you actually submit the form.

 

I will give that a shot now. I figured that the variable remained null for that reason.

Link to comment
Share on other sites

I know VERY LITTLE about javascript and am having trouble now with capturing the ID of the button that was pressed.

 

With the form that I am attempting to create, what is wrong with the below javascript?

 

<script type="text/javascript">
<!--
var section = document.getElementByName("deny_profile_approval").value;
function deny_approval() {
window.open( "deny_approval.php?section=+section", "myWindow", 
"status = 1, toolbar = no, scrollbars = yes, location = no, resizable = no, height = 600, width = 600, resizable = 0" )
}
//-->
</script>

 

The form has been edited to the following:

 

<form action="" method="post" enctype="multipart/form-data">

<input name="deny_profile_approval" type="button" onclick="deny_approval()" value="Deny Contact Info1" /><br /><br />
<input name="deny_profile_approval" type="button" onclick="deny_approval()" value="Deny Contact Info2" /><br /><br />
<input name="deny_profile_approval" type="button" onclick="deny_approval()" value="Deny Contact Info3" /><br /><br />
<input name="deny_profile_approval" type="button" onclick="deny_approval()" value="Deny Contact Info4" /><br /><br />

</form>

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="js/jquery-1.5.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function(){

$(".approval").click(function(){

v = $(this).attr("value");
url = 'deny_approval.php?section=' + v;

window.open(url, "myWindow", 
"status = 1, toolbar = no, scrollbars = yes, location = no, resizable = no, height = 600, width = 600, resizable = 0" );

});

});




</script>

</head>

<body>
<form action="" method="post" enctype="multipart/form-data">

<input class="approval" type="button"  value="Deny Contact Info1" /><br /><br />
<input class="approval" type="button" value="Deny Contact Info2" /><br /><br />
<input class="approval"  type="button" value="Deny Contact Info3" /><br /><br />
<input class="approval" type="button"  value="Deny Contact Info4" /><br /><br />

</form>

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="js/jquery-1.5.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function(){

$(".approval").click(function(){

v = $(this).attr("value");
url = 'deny_approval.php?section=' + v;

window.open(url, "myWindow", 
"status = 1, toolbar = no, scrollbars = yes, location = no, resizable = no, height = 600, width = 600, resizable = 0" );

});

});




</script>

</head>

<body>
<form action="" method="post" enctype="multipart/form-data">

<input class="approval" type="button"  value="Deny Contact Info1" /><br /><br />
<input class="approval" type="button" value="Deny Contact Info2" /><br /><br />
<input class="approval"  type="button" value="Deny Contact Info3" /><br /><br />
<input class="approval" type="button"  value="Deny Contact Info4" /><br /><br />

</form>

 

This is amazing thanks. I will test it now. Meanwhile, do you mind explaining this to me? I don't mean to be a bother but I am extremely interested in understanding this.

Link to comment
Share on other sites

This is amazing thanks. I will test it now. Meanwhile, do you mind explaining this to me? I don't mean to be a bother but I am extremely interested in understanding this.

 

Sure. Because there is no "real" form submitting going on here the input elements could just as well be buttons or anchors. You can remove all the form markup.

 

The jQuery (Javascript) listens to all click events on all elements that have the class approval (in this case its those input elements). When one is clicked, we extract the value attribute (Deny Contact Info[1,2,3,4]) and then call the window.open function with that value passed in the URL.

 

$(document).ready(function(){

$(".approval").click(function(){

v = $(this).attr("value");
url = 'deny_approval.php?section=' + v;

window.open(url, "myWindow", 
"status = 1, toolbar = no, scrollbars = yes, location = no, resizable = no, height = 600, width = 600, resizable = 0" );

});

});

Link to comment
Share on other sites

Pure genius!!! I totally understand this now. The syntax was throwing me off for a bit but I get it. The reason the input button didn't need the 'onclick' attribute added to it was because you stated in the javascript that any element with the class 'approval' would activate the function if 'click'...wow this is amazing!!! Thanks again man.

 

Javascript has always been something that I've wanted to learn. Now that I am becoming rather fluent in PHP and finally realizing its limitations, I realize even more the importance to grasp a full understanding of both of these languages. Once again, I can't thank you enough.

 

Bl4ck Maj1k 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.