-1

i have two page. first one give information about user and second one sends them to database table.

i wonder if i can put all codes in just one page because of i have too many page and some times and i should search for find my pages. here is two pages that are related together:

submit_job.php:

   <fieldset class="fdex" >
        <legend><span class="style4">تعیین شغل</span></legend>


        <?php

   $db_host = 'localhost';
   $db_name= 'site';
   $db_table= 'job_list';
   $db_user = 'root';
   $db_pass = '';


 $con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");

 mysql_query("SET NAMES 'utf8'", $con);
 mysql_query("SET CHARACTER SET 'utf8'", $con);
 mysql_query("SET character_set_connection = 'utf8'", $con);

 $selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
 mysql_query("SET CHARACTER SET  utf8");
 $dbresult=mysql_query("SELECT * FROM  $db_table",$con);
 echo "توانمندی مورد نظر خود را انتخاب نمایید: ";?><br/>
 <form name="form2" method="post" action="send.php" accept-charset='UTF-8'>

 <?php
 echo '<select name="job">';
 ?>
 $user_id=<?= $fgmembersite->UserFullname(); ?>

 <?php

 while($amch=mysql_fetch_assoc($dbresult))
 {
 echo '<option value="'.$amch['job_id'].'">'.$amch['job_name'].'</option>';
 }
 echo '</select>'; 
 ?><br/>

 <textarea name="textaria" cols="" rows=""></textarea>
          <input name="submit" type="submit" value="submit" />

and also second page contents:

 <?php
 $user_id =$fgmembersite->UserID(); ?>
<?php 
 echo "$user_id ";
 ?>



 <?php
 $db_host = 'localhost';
 $db_name= 'site';
 $db_table= 'relation';
 $db_user = 'root';
 $db_pass = '';


  $con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");

  mysql_query("SET NAMES 'utf8'", $con);
  mysql_query("SET CHARACTER SET 'utf8'", $con);
  mysql_query("SET character_set_connection = 'utf8'", $con);

  $selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
   $ins = "INSERT INTO $db_table (job_id,user_id,comments) VALUES ('" . mysql_escape_string($_POST['job']) . "','$user_id ', '" . mysql_escape_string($_POST['textaria']) . "')";
 $saved=mysql_query($ins );
 mysql_close($con); 
 echo "('" . mysql_escape_string($_POST['job']) . "')";
 ?>

i just want a way to merge these pages in only one page. it is better than have too many pages.

sajad
  • 1,012
  • 7
  • 13
  • are all the fields required in your form? – N3R4ZZuRR0 Nov 04 '15 at 13:24
  • 2
    sure you can, and use a conditional statement; *done like dinner* – Funk Forty Niner Nov 04 '15 at 13:25
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Nov 04 '15 at 13:31
  • 2
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 04 '15 at 13:31
  • I am pretty sure you have not included all of these 2 files as I can see nowhere where `$fgmembersite` is defined/instantiated. So I will resist the temptation of merging the 2 files for you. – RiggsFolly Nov 04 '15 at 13:32

3 Answers3

1

You can use require() or include() function in php.
Save your code in different file like file1.php and file2.php
then your submit_job.php will be

<?php
   require('file1.php');
   require('file2.php');
?>

You can also use include.
Different between include and require, is that page will not process if it does not find the files in case of required.

Swap
  • 165
  • 3
  • 12
  • 2
    That does not answer the question and this would mean instead of 2 files he would now have 3. He wanted just ONE file. – RiggsFolly Nov 04 '15 at 13:30
1

The canonical "simple" of a single-script-does-it-all method is something like this:

<?php

... connect to database

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ... form was submitted, process it
   if (no errors found) {
      ... save to database
      ... display success stuff
      ... exit/redirect
   }
}

<form action="" method="POST">
... display form, display any errors from POST processing
... if displaying errors, use previously submitted data to repopulate form for editing.
</form>

Basically: You have a code section that handles the form submission. If everything goes ok there, you save the data, and send the user somewhere else. If there were errors, you simply continue on to the form display section. The form submits to itself, and that section can display any errors generated by the POST processing, and (re?)display the form for the user to fill out or correct.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1
<?php
// DB Connect
if(($_POST['form2'])&&($_POST['job'])&&($_POST['textaria'])) {
    // Display the stored record
}
// Form for adding more records
?>
N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
  • While this may provide an answer to the question, it helps to provide some commentary so others can learn "why" this code does what it does. Please see [How to answer](http://stackoverflow.com/help/how-to-answer) for ways to add more detail/context to an answer. – Glen Selle Nov 04 '15 at 19:26