Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

how to send variables

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

how to send variables

Postby rafar20 » Mon Feb 04, 2013 6:54 pm

Hello, I'm making a project for my course and it is about a school platform. I'm making an studdents insert page and it contains 2 steps:
- Step 1 is the studdent information;
- Step 2 is the parent information.

The way I'm doing this is by using the same php file "addstuddent.php" and what I have is something like this:

1st: When I press "Add Studdent" on the previous page I will be redirected for "addstuddent.php?step=1"
2nd: When I press the submit button "Next" I want to check for errors on the fields and if everything is fine it redirects to "addstuddent.php?step=2"

the code is:

Code: Select all
<?php

if($_REQUEST['step'] == 1)
{
   <form action="addstuddent.php?step=1" method="POST">
      <input type="text" name="fullname">
      <input type="text" name="birthday">
      <input type="text" name="address">
      <input type="submit" value="Next" name="next">
   </form>

   if(isset($_REQUEST['next']))
   {
      if($_REQUEST['fullname'] != NULL && $_REQUEST['birthday'] != NULL && $_REQUEST['address'] != NULL))
     {
        ?><script javascript> window.location="addstuddent.php?step=2"</script><?php
     }
   }
}

if($_REQUEST['step'] == 2)
{
    echo $_REQUEST['fullname']; //__this won't work and I know it doesn't
}
?>


I will get this error Notice: Undefined index fullname

Could someone help me and tell me how could I make it work? to send the variables from that form to the step 2 ?
rafar20
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Feb 04, 2013 6:28 pm

Re: how to send variables

Postby simplypixie » Mon Feb 04, 2013 10:03 pm

There are a few errors with your script

1. $_REQUEST is deprecated so shouldn't be used - use $_GET and $_POST instead

2. You can't just put html elements in the middle of PHP (i.e. you form), you need to close the PHP tags and re-open them again

3. When you send the page using your javascript it doesn't retain the posted information so you need to add it to the url and use $_GET. Based on your script you should be using PHP's header redirect instead anyway.

However, you can do this much more easily and cleanly using the script below
Code: Select all
<?php
if (isset($_GET) && $_GET['step'] == 1) {
  if (isset($_POST) && !empty($_POST['fullname']) && !empty($_POST['birthday']) && !empty($_POST['address'])) {
    echo $_POST['fullname'];
  } else {
?>
<form action="addstuddent.php?step=1" method="POST">
      <input type="text" name="fullname">
      <input type="text" name="birthday">
      <input type="text" name="address">
      <input type="submit" value="Next" name="next">
   </form>
<?php
  }
}
?>
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron