Jump to content

Recommended Posts

I am trying to make it so if a PHP session entity exists, then JavaScript will enable a button.

 

I am having trouble getting this to work and wondering if anyone knows what I am doing wrong?

 

function purchaseEnable() {
if ($_SESSION['order']['paymentmethod']) {
	document.getElementById("purchase").disabled = false;
}
}

<INPUT disabled id="purchase" type="submit" value="PURCHASE">

Link to comment
https://forums.phpfreaks.com/topic/256167-button-wont-enable/
Share on other sites

Ow I C... Then I'm going to have to get tricky arn't I LOL!

 

function purchaseEnable() {

<?php if ($_SESSION['order']['paymentmethod']) {

echo 'document.getElementById("purchase").disabled = false;'; ?>

}

}

 

Not test, but will do now until I get it working. lol if I'm not back in an hour, this is solved.

 

Thanks for your information.

Link to comment
https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313225
Share on other sites

I am now using this:

 

<?php
if ($_SESSION['order']['content']) {
	echo '
		<SCRIPT type="text/javascript">
			function purchaseEnable() {
				if (document.getElementById("agree") == "Yes") {
					document.getElementById("purchase").disabled = false;
				}
			}
		</SCRIPT>
	';
}
?>

<SELECT id="agree">
<OPTION></OPTION>
<OPTION>Yes</OPTION>
</SELECT>

<INPUT disabled id="purchase" type="submit" value="PURCHASE">

 

Its still not working. I take it I have stuffed up with JavaScript some where and trying to do something thats not allowed.

 

Can anyone see a mistake?

Link to comment
https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313229
Share on other sites

Heres a way to check select options and only alerts if yes is selected

 

<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
window.onload = function(){
var s1 = document.getElementById("s1")
s1.onchange = function(){
for(var i = 0; i < s1.options.length; i++){
	if(s1.options[i].selected && s1.options[i].value == "yes"){
		value = s1.options[i].value;
		alert(value);
	}
}
}
}
</script>
</head>
<body>
<form>
<select id="s1">
<option>---</option>
<option value="yes">yes</option>
<option>no</option>
</select>
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313247
Share on other sites

Thanks, but that won't be necessary. I am now 100% the problem lays with this line:

 

if (document.getElementById("agree").value == "Yes") {

 

When I remove the above piece of code, the button is no longer disabled, and everything else is work so this must be it.

Now the big question... why isn't it working?

 

Here is all the code it works in conjunction with. Maybe someone who is more experienced than me can spot something I can't.

 

<SCRIPT type="text/javascript">
if (document.getElementById("agree").value == "Yes") {
	document.getElementById("purchase").disabled = false;
}
</SCRIPT>

<FORM action="processorder/" method="post">
<DIV>
	Do you agree to the terms of sale? 	<SELECT id="agree">
							<OPTION></OPTION>
							<OPTION>Yes</OPTION>
						</SELECT>
	<INPUT disabled id="purchase" type="submit" value="PURCHASE">
</DIV>
</FORM>

Link to comment
https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313252
Share on other sites

Your issue is that the statement below the element doesnt have a value as you have targetted the select tag and not the options tags inside it do.

 

if (document.getElementById("agree").value == "Yes")

 

just implement my snippet of code into your script and it will work. Basically it runs through all the option tags inside the select with a for loop and IF the option is selected and the value is value yes only then will the code go on or in my case alert u.

Link to comment
https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313255
Share on other sites

Ok, i assumed all you wanted to do now was retrieve a value from a option tag and check the value is yes? I just gave you a dynamic way of check but you can make it more direct by removing the for loop.

 

PS,

More direct approach by selecting option from its array.

 

var s1 = document.getElementById("s1")
s1.onchange = function(){
if (s1.options[1].selected){
	var optionVar = s1.options[1].value;
}else{
                var optionVar = null;
        }
}

 

The 1 corresponds the position of the option inside the select tag starting a 0.

Link to comment
https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313266
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.