0

Is it possible to reload a parent php file WITHOUT javascript ?I dont wanna use Javascript because if the client has disabled Javascript .

So I have a object and there should be a button to change the background color of the main page

parent.php:

<!doctype html>
<html>
  <head>
    <title>This is the title of the webpage!</title>
    <?php 
      session_start();
      $_SESSION['backgroundcolor'] = '#fcba03';
    ?>
  </head>
  <body style="background-color:<?php echo $_SESSION['backgroundcolor'];?>">
    <object data="changebackground.php" width="500" height="200">
    </object>
  </body>
</html>

changebackground.php:

<form action="" method="post">
  <input type="submit" value="change bgcolor" name="submit">
</form>

<?php
$var = "#ff0000";

if (isset($_POST['submit'])) {
  $_SESSION['backgroundcolor'] = $var;
}
?>
6
  • 1
    Yes, but it involves a round trip to the server, because PHP only runs in the server, not in the browser Commented Jan 7, 2022 at 14:19
  • NOTE If you dont have any code to go in an ELSE, then you dont need to code an empty ELSE Commented Jan 7, 2022 at 14:19
  • The likelihood of someone having JavaScript disabled in the current world is very, very small, to the point that I wouldn't concern yourself with it. For better or worse, large chunks of the internet aren't usable without it. But, if this is a big concern, you can require JS for your application and have a <noscript> block that tells people at the start that they need to have JS enabled to use your site. Commented Jan 7, 2022 at 14:29
  • Why not use a <form> for this? Commented Jan 7, 2022 at 14:53
  • @NicoHaase How could I use <form> Commented Jan 7, 2022 at 14:54

2 Answers 2

1

To answer your literal question, the only solution I can think is to use the target element in either a link or a form (_top should work but _parent is probably more accurate):

  • _self: the current browsing context. (Default)
  • _blank: usually a new tab, but users can configure browsers to open a new window instead.
  • _parent: the parent browsing context of the current one. If no parent, behaves as _self.
  • _top: the topmost browsing context (the "highest" context that’s an ancestor of the current one). If no ancestors, behaves as _self.

How to fit this with your current logic is a different subject. The main issue is that these elements require a manual user intervention: user has to click to trigger the action since we don't want JavaScript to do it for him. Thus you need to merge the change colour form with the reload parent frame feature. The most obvious way is to perform the colour changing action at parent.php.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked I marked your answer as right and my question as solved I made a own anser with the modified script
0

Thanks to Álvaro González , I now solved the problem this is my Current script

Parent.php

<!doctype html>
<?php session_start();?>
<html>
  <head>
    <title>Change Backgroundcolor with pure PHP</title>
<?php 
          
      if(empty($_SESSION['backgroundcolor'])){
        $_SESSION['backgroundcolor']='#fcba03';
      }else{
      }
    ?>
 </head>
  <body style="background-color:<?php echo $_SESSION['backgroundcolor']; ?>;" >
  <?php 
    

    echo $_SESSION['backgroundcolor'];
    echo '<object data="changebackground.php" width="500" height="200">';
    
  ?>
    
          
    </object>
  </body>
</html>

changebackground.php

<form action="" method="post" target="_parent">
<input type="submit" value="change bgcolor" name="submit">
</form>

<?php
  session_start();  
  $var= "#ff0000";
if (isset($_POST['submit']) ) {
    $_SESSION['backgroundcolor']=$var;
    header("Refresh:0; url=parent.php");
};

?>

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.