0

I'm trying to build a shift generator as a practice for my web development studies.

So far I've managed to retrieve from the user the number or working people he would like to arrange.

For the next step I want to create an array of the names on the size that I was given. that's where I'm stuck, what should I use to do it? PHP? JS? how do I use the number I got on $_GET with JS?

The code:

<?php
  print_r($_GET);   
?>
<html>
<body>
    <p>Hey there! how many working guys would you like to arrange?</p>

    <form action="">
        <input name="numOfWorkers" type="text"> 
        <input type="submit" id="input">    
    </form>
    <script type="text/javascript">
        var names = new array(); 
        var numOfWorkers = $_GET['numOfWorkers'];
    </script>
</body>
</html>
Joundill
  • 6,828
  • 12
  • 36
  • 50
Ofir Ozery
  • 19
  • 5
  • 1
    FYI: `type="text/javascript"` is not needed on the `script` element as that is the default `type` (it has been for many years now). – Scott Marcus Sep 20 '20 at 22:59
  • PHP executes on an HTML page before it is send to the Browser. It needs to be at least in PHP tags like: `let numberOfWorkers = =$_GET['numberOfWorkers']?>;`. Of course, I don't usually recommend that you write JavaScript with PHP. You want your JavaScript cached, so put it in your head with an external `src` attribute. Keep it external and use `FormData` along with the `XMLHttpRequest`. – StackSlave Sep 20 '20 at 23:14
  • 1
    to be clear `PHP executes on an HTML page` is misleading. PHP generates a HTML page which is sent to the browser, but it can also generate other types of response other than HTML – Jaromanda X Sep 20 '20 at 23:35
  • Would using `location.search` be more helpful? – evolutionxbox Sep 20 '20 at 23:48
  • 1
    I would suggest using `method="post"` on your form tag, then when the submit button is pressed, the data will be in the post and retrievable using `$_POST['numOfWorkers']`. Then you can cleanse the data and use it however you wish by recalling that `$_POST` variables value. No need for javascript unless you want to do something with the information BEFORE the user refreshes the page by submitting the form button. Maybe for an input validation of some kind, otherwise, just use PHP. – dale landry Sep 21 '20 at 00:00
  • First of all thank you all for responding! I still have trouble understanding how to create the array using PHP, I would need to have the user enter the names via a form, but a form works on HTML, not PHP – Ofir Ozery Sep 21 '20 at 00:13
  • A form works on a php page... Your php code would be run on top of the html within the php page... – dale landry Sep 21 '20 at 00:14
  • I got a little dizzy there LOL. did you mean that the all code would be in HTML and the PHP within it? or have the HTML within the PHP? – Ofir Ozery Sep 21 '20 at 00:19
  • You have a php page... `somepage.php`, you initialize your HTML as you would on an HTML page, instead the file extension is php. The php cope would go in a php block on top of the HTML code or at the top of the page, typically. – dale landry Sep 21 '20 at 00:22
  • Ok, I used the post method as you suggested, but still not clear about how to retrieve an array of names from the user using PHP (Sorry for the lack of knowledge, and thank you for the patience and replies) – Ofir Ozery Sep 21 '20 at 00:32
  • I will give you an example of how this would work in PHP in the answers section... – dale landry Sep 21 '20 at 00:46
  • See my answer, remember this is pretty basic in presentation but should illustrate how one could gather a number of workers, use that number to then gather their names, then store that information into an array using php... Hope it helps... – dale landry Sep 21 '20 at 01:14
  • Its an amazing help! thank you so much again – Ofir Ozery Sep 21 '20 at 02:00

1 Answers1

1

This is an example of how to collect dynamic information like workers from a form using PHP. Remember that php is a backend language and runs when the page refreshes, like when you hit the submit button on a form set to method POST, GET or REQUEST. The method is sent to the server and the global variable is created holding the name values of the form.

You retrieve the POST methods using $_POST['name-value'].

IMPORTANT NOTE: Always cleanse your POST values with some kind of relevant cleansing function. I have NOT included any cleansing functions in my example!

See This Link on S.O. for more information on sanitation of inputs.

If the page is currently an html file extension, you must change it to .php file extension.

Hope this helps!

<?php
$namesOfWorkers = []; // initialize a NEW empty array in PHP
$output = ''; // initialize an empty variable to hold the output into the HTML

// is the $_POST global 'numOfWorkers' key set?
// If not, run through the php conditional to the next elseif section
if(isset($_POST['numOfWorkers'])){ 
  $numOfWorkers = $_POST['numOfWorkers'];
  // set an increment and increment each time we loop through 
  // the amount of workers set in numOfWorkers
  $output .= "<form method='post'>"; // create the form html
  for($i = 1; $i < $numOfWorkers; $i++){ // runt he incremented for loop against the number set by user
    // now use that number to dynamically create input fields 
    // that would gather the names of the workers... 
    
    $output .= 'Name '.$i.' : <input class="namesOfWorkers" name="nameOfWorkers'.$i.'"><br>';
  }
  $output .= '<input type="submit" name="nameSubmit" id="nameSubmit">
           </form>';
// $output now holds the entire new form that gathers the amount of names needed 
// to match the number initially entered by user...

// now we see if the names form has been submitted, if so, we push the values into the array
}elseif(isset($_POST['nameSubmit'])){
  $j = 0; // another increment for the keys into the array
  foreach($_POST as $key => $value){
    // lets NOT include the submit buttons key in our array... 
    if($key !== "nameSubmit"){
      // push the number into the key and the $_POST arrays value into the array value
      $namesOfWorkers[$j] = $value;
      $j++; // increment $j by one with each loop
    }
  }

}else{
  // the code that starts off our form
  $output = '<p>Hey there! how many working guys would you like to arrange?</p>

  <form method="post">
      <input name="numOfWorkers" type="text"> 
      <input type="submit" id="input">    
  </form>';
}
// $namesOfWorkers now holds the array of your workers key/value(names) pairs
?>

Further down on the SAME PHP page, include the HTML

<!DOCTYPE html>
<html lang="en">
  <body>

  <?=$output?> 
  <!--// <?= is a short tag for echo to echo out the $output variable which 
       Dynamically changes each time we run through the if conditional 
       within PHP, <? echo $output; ?> could also be used //-->

  </body>
</html>

NOTE: To further assist in how this all works, you may want to run a print_r($_POST) or var_dump($_POST) at each interval of the conditional if/else statement to see the POST global arrays key/value pairs and study its output against that block of code...

dale landry
  • 7,831
  • 2
  • 16
  • 28
  • I've gone through it a few times, its a lot to for me to process haha, thank you so much, I hope I can repay somehow :) I am honestly overwhelmed by the generosity, thank you so much! – Ofir Ozery Sep 21 '20 at 01:33
  • No worries man, coding php is not easy but its not as hard as you think it may be, just need to set up a server on you PC and test things out... – dale landry Sep 21 '20 at 02:04