0

I am sorry if has any unclear sentences beacuse I am bad in explaining thingy..

So i have three sample file name.html, time.html and insert.php.. in name.html, after i click the button and it will pop up another window (time.html). Then in time.html after i click the submit button, the insert.php will execute.

name.html

<form method = "post">
   <h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
   <input type="button" name="submit">
</form>

time.html

<form method="post" action="insert.php">
   <input type="time" name="time_name" id="my_time">
   <br><br>
   <input type="submit" name="myButton" id="myButton">
</form>

insert.php

<?php
   $name = $_POST['student_name'];
   $time = $_POST['time_name'];

   if(mysqli_query($connect, "INSERT INTO student (student_name, time) 
VALUES ('$name', '$time')")){
   ....}else{ ...}

My question: Is there a way that the insert.php can retrieve both name and time information in the html files but not only in time.html.

If I add in the insert.php file in name.html, the php file will execute first.

<form method = "post" action= "insert.php">
   <h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
   <input type="button" name="submit">
</form>



::Most of the sentence makes no sense, if any unclear sentence please let me know.. Also, i couldn't think any better title for this question..



Edit: Seems like i found the solution.. I just add an action in the name.php.

In name.html i change to name.php

<form method = "post" action = "<? include 'time.php' ?>" >
   <h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
   <input type="button" name="submit">
</form>

In time.html change to time.php

Thanks for helping me out!!

  • don;t you need to configure .htaccess for php to work with .html extensions? – Jurick Pastechi Genaro Oct 04 '17 at 14:12
  • Let me rephrase some just to make sure. You got two forms, one asking for student name, another asking for a time and you want to have the second form send the value of the first form with it... Right? – Salketer Oct 04 '17 at 14:13
  • `it will pop up another window`. Can you show how your are opening another window ? – Panther Oct 04 '17 at 14:20
  • My solution for this will include modal and ajax so that you can play around with two forms in 1 html file. Since both student_name and time_name are found on two different forms. Your PHP can only access the data submitted to it by one form. The only work around I know is to use ajax. – hungrykoala Oct 04 '17 at 14:20
  • You could make `time.html` into a `php` file then grab the `$_POST` vars from the previous form and add them as hidden form fields in the new form before submitting it... or you could have the *time* insert in a modal window (JS display toggle) instead of opening a new file - you've only got one form then. Or use a one-step accordion style form rather than popups. There are plenty of ways around this issue. – CD001 Oct 04 '17 at 14:20
  • @Panther var myWindow = window.open("", "MsgWindow", "width=291,height=258"); – Sparta King Oct 04 '17 at 14:28
  • @hungrykoala im pretty new to this php, but i heard about AJAX.. will look for it.. Thank you! – Sparta King Oct 04 '17 at 14:30
  • @CD001 Okay.. i think i understand a little bit.. will try to solve it... Thanks you!! – Sparta King Oct 04 '17 at 14:32
  • @Salketer yeah.. thats what i meant for!! – Sparta King Oct 04 '17 at 14:34

2 Answers2

0

Your question: Is there a way that the insert.php can retrieve both name and time information in the html files but not only in time.html.

If I add in the insert.php file in name.html, the php file will execute first.

The way you explained is a bit confusing ! but anyway if you want include the php to your name.html !

1. Either instruct Apache to treat .html files as PHP(See This) or make it as a .php file

and if you want to restrict your php code from executing first or automatically ! update your code with

if(isset($_POST['something'])){ //all other stuff here }

On your code

<?php
if(isset($_POST['submit'])) { //executes only when the submit button clicked !
   $name = $_POST['student_name'];
   $time = $_POST['time_name'];

   if(mysqli_query($connect, "INSERT INTO student (student_name, time) 
VALUES ('$name', '$time')")){

   ....}else{ ...}

You can also make some more conditions to check whether $name & $time values are entered or not also simply you can validate it with making it as required in html itself

Also in a better way you can do the same with AJAX and some JS See the answer here

Akshay N Shaju
  • 355
  • 4
  • 17
0

You have two files with two forms. And you have 1 php file to process both. And you want the data from both files in your insert.

Then why don't you create only 1 file, 1 form, ask for both info, insert once.

So your form would become:

<form action="index.php" method="POST">
    <h3>Name: </h3><input type="text" name="student_name" id="studentName">
    <h3>Time: </h3><input type="time" name="time_name" id="my_time">
    <input type="button" name="submit">   
</form>

Obviously this above code is to illustrate, I do not take care of everything. Then you have to check SQL injection, validate your inputs, ...

As far as I know, there is no way to get data from 2 forms into a single action, since every time you click submit, it executes the action.

Unless your first form defines it's action as the second form. Then the second form creates a to pass the result of the first form along with it's input to the action file that does the insert to database.

Nic3500
  • 8,144
  • 10
  • 29
  • 40