0

I want to enable or disable the buttons from page2.php from page1.php. when i click on the submit button from page1.php it should ENABLE/DISABLE button in the page2.php without redirecting to page2.php.

here am trying to send value from page1.php to page2.php using javascript. but unable to get data.

here is what i have tried.

page1.php

<html>
<head>
<script type="text/javascript">
function enable(){

var enable="enable";
window.location = "page2.php?temp=" + enable;
alert(enable);
}

</script>
</head>
<body>

click button to ebable or disable

<input type="submit" name="submit" id="submit" value="enable"onclick="enable();"> 

</body>
</html>

and this is my page2.php

<html>
<head>
<script type="text/javascript">
function enable(){

var temp=Request.querystring["temp"];

alert(temp);
}

</script>
</head>
<body>

click button to ebable or disable
<br>
<input type="submit" name="submit" id="submit" value="enable" onclick="enable()"  > 

</body>
</html>

can anyone suggest how to get it.?

Ajeet Kumar
  • 805
  • 1
  • 7
  • 26
Sagar Pawar
  • 465
  • 4
  • 26
  • In page 2 where you have taken data from url? and I think... you have to call enable function on page load. – Ajeet Kumar Oct 29 '15 at 11:13
  • Request.quesrystring["temp"] will fetch data from first page. but that's not working. – Sagar Pawar Oct 29 '15 at 11:15
  • Your question is not much clear. Also the code is very confusing. Do you want to disable a button on page1.php and it should show disabled on page2.php when visited? You said the apge should not redirect but you are redirecting it using window.location. – Ashish Choudhary Oct 29 '15 at 11:17
  • Reuest.Querystring is not a native function in javascript are you using any library – Virendra Yadav Oct 29 '15 at 11:17
  • sorry for that. let me make it clear. when i click a button on page1.php, it should disable the button on page2.php. – Sagar Pawar Oct 29 '15 at 11:18
  • http://stackoverflow.com/questions/814613/how-to-read-get-data-from-a-url-using-javascript have you seen this – Ajeet Kumar Oct 29 '15 at 11:20
  • @AjeetKumar this post is very heavy. am unable to understand it. – Sagar Pawar Oct 29 '15 at 11:31

4 Answers4

0

I think you can set

var x = document.getElementById("button").setAttribute("value", "enable");

Like that, you can access that specific variable and edit it's enability.

And using the javascript, you can pass in the value of the button's enability to the second PHP page.

  • do i to have to write this in page1.php ? – Sagar Pawar Oct 29 '15 at 11:35
  • can i just ask why you're using javascript instead of using the get or post attribute using the input tag? –  Oct 29 '15 at 11:38
  • i don't want to redirect to page2.php when i submit data from page1.php. if i use get or post method, it will redirect to page2.php and i don't want that. – Sagar Pawar Oct 29 '15 at 11:40
  • But, to submit, or file a "request", you would need to redirect to page 2 –  Oct 29 '15 at 11:40
  • There are plenty of tutorials of GET and POST method php in google. –  Oct 29 '15 at 11:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93675/discussion-between-muymuy1-and-sagar-pawar). –  Oct 29 '15 at 11:45
0

in page2.php inside function enable after you get temp value

if(temp){
    document.getElementById("myBtn").disabled = true;
} else {
    document.getElementById("myBtn").disabled = false;
}
0

Try this:

NOTE: Please Read the comments in the code for more explanation.

page1.php and page2.php will have JS code like below:

<script type="text/javascript">
function enable(){
    // Set enable/disable in session to be read on next page and this page on refresh
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "setButtonStatus.php", true);
    xhttp.send();

    // set enable/disable value for current event
    if(this.value == 'enable'){
        this.value = 'disable';   
    } else {
        this.value = 'enable';
    }
}
</script>

In setButtonStatus.php

<?php
// setButtonStatus.php
// Set button status as enable in session if not already set or if disabled
if(isset($_SESSION['setButtonStatus']) && $_SESSION['setButtonStatus'] == 'enable'){
    $_SESSION['setButtonStatus'] = 'disable';
} else {
    $_SESSION['setButtonStatus'] = 'enable';
}
?>

page1.php HTML code

<input type="submit" name="submit" id="submit" value="<?php echo isset($_SESSION['setButtonStatus']) ? $_SESSION['setButtonStatus'] : 'enable' ; ?>" onclick="enable()"  >

page2.php HTML code

<input type="submit" name="submit" id="submit" disabled="<?php echo isset($_SESSION['setButtonStatus']) && $_SESSION['setButtonStatus'] == 'disabled' ? true : false ; ?>" value="<?php echo isset($_SESSION['setButtonStatus']) ? $_SESSION['setButtonStatus'] : 'enable' ; ?>" onclick="enable()"  >
Ashish Choudhary
  • 2,004
  • 1
  • 18
  • 26
0

change your code as below page1.php

<html>
<head>
<script type="text/javascript">
function enable(){

var enable="enable";
alert(enable);
window.location = "page2.php?temp=" + enable;
}

</script>
</head>
<body>

click button to ebable or disable

page2.php

<html>
<head>
</head>
<body>
<style>
#submit{
   display:none;
}
</style>
while page will load button is hide but if in url temp= enable then     buton will be visible
 <input type="button" value="button here" name="submit" id="submit"   <?php if($_GET['temp']=='enable'){ echo "style='display:block'";} ?>> 

 </body>
  </html>