Home > Mobile >  How to include data from a php file without include function
How to include data from a php file without include function

Time:03-10

I am working on a signup page in html and PHP. When all is correct, it should redirect you to another file. I have two files, one is techer.php the other indexa.php. I want redirect from techer.php to indexa.php and then include techer.php to get some data from it. I thought the way to go about this was include("techer.php") but when I do that, I would get stuck in a redirect loop and then it will say: localhost redirected u too many times.

This is techer.php:

<?php
$servername = "loc";
$username = "$$$$$$";
$password = "$$$$";
$dbname = "demoq";


$name = $_GET['username'];
$skool = $_GET['skool'];
$email = $_GET['email'];
$pwd = $_GET['password'];

$hashed = password_hash("$pwd", PASSWORD_DEFAULT);
mkdir($skool, 0700);
$myfile = fopen("$skool/index.html", "w");
$myfile1 = fopen("$skool/index.css", "w");
$myfile2 = fopen("$skool/submit.php", "w");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO `teacher` (`School`, `Email Address`, `Password`, `Username`)
VALUES ('$skool', '$email', '$hashed', '$name')";

if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

echo file_put_contents("eacher.html","Hello World. Testing!");

$conn->close();
header("Location: /school/choose/indexa.php");
exit();
?>

and then, of course, the is indexa.php:

<!doctype HTML>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="indexa.css">
<script src="https://kit.fontawesome.com/2b5f6cfacf.js" crossorigin="anonymous"></script>
</head>
<body>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="index.css">
<script src="https://kit.fontawesome.com/2b5f6cfacf.js" crossorigin="anonymous"></script>
</head>
<body>
<button style="border-radius:75%;" onclick="custom()"><img id="user" width="25" height="25" src="user-plus-solid.svg"></img></button>
<div  id="div">
<p>Cusotmise your Profile</p>
<input type="file" accept=".jpg, .gif, .png" onchange="loadFile(event)"></input>
</div>
<div >
<div >
    <button >Main
      <i id="i" ></i>
    </button>
    <div id="dropdown-content" >
       <div >
    <button  onclick="seven()">1
      <i ></i>
    </button>
    <div  id="further">
      <button ><span id="span1">7A</span>
      <i ></i>
    </button>
    <button onclick="edit1()">Edit</button>
    <div  id="input1">
    <input id="inp1"></input><button onclick="save1()">Save</button>
    </div>
    <div id="chatfor7a">
    <p>Chat for Class<span id="classspana">7A</span>
    <form action="">
    <div >
    <div >
    <p>Looks like there is nobody here. At the moment. Invite the students!</p>
    </div>
    </div>
    <div >
    <input placeholder="Send a Message"></input>
    <button type="submit">Send ></button>
    </div>
    </div>
    <button ><span id="span2">7B</span>
      <i ></i>
    </button>
    <button onclick="edit2()">Edit</button>
    <div  id="input2">
    <input id="inp2"></input><button onclick="save2()">Save</button>
    </div>
    <button ><span id="span3">7C</span>
      <i ></i>
    </button>
    <button onclick="edit3()">Edit</button>
    <div  id="input3">
    <input id="inp3"></input><button onclick="save3()">Save</button>
    </div>
    <button ><span id="span4">7D</span>
      <i ></i>
    </button>
    <button onclick="edit4()">Edit</button>
    <div  id="input4">
    <input id="inp4"></input><button onclick="save4()">Save</button>
    </div>
    </div>
    </div>
    
    <div >
    <button  onclick="eight()">2
      <i ></i>
    </button>
    <div  id="furtherr">
      <button >8A
      <i ></i>
    </button>
    <button >8B
      <i ></i>
    </button>
    <button onclick="add2()"><img src="circle-plus-solid.svg" width="20" height="20"></img></button>
    </div>
    </div>
    <div >
    <button >3
      <i ></i>
    </button>
    </div>
    <div >
    <button >4
      <i ></i>
    </button>
    <div >
    <button >4 again
      <i ></i>
    </button>
    </div>
    </div>
    </div>
    </div>
    <script src="indexa.js"></script>
</body>
</html>

CodePudding user response:

It seems to me the solution to your immediate problem is very simple: create a third file.

You have some code in "techer.php" which you want to run again in "indexa.php", and some which you don't. So move the code you want to run in both to a new file, say "getdata.php", and then include that from both places.

I would also encourage you to find a tutorial that teaches you how to structure your code into functions and classes. Then, rather than just using include to run exactly the same code in more than one place, you can write a function with parameters to perform similar code in more than one place.

CodePudding user response:

Others have already pointed out some security concerns so skipping that.

You can combine the two files and wrap the code from teacher.php in a conditional like

if (!empty($_POST)) {
 // teacher.php code here
}

and remove

header("Location: /school/choose/indexa.php");
exit();

Then you can use the values of the POST variable for use on that page with $_POST. Reference https://www.php.net/manual/en/reserved.variables.post.php

Hope that helps, do consider the comments people made about security.

  •  Tags:  
  • php
  • Related