Results 1 to 2 of 2

Thread: Question about "if(isset"

  1. #1
    Join Date
    Jan 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about "if(isset"

    Hi,
    I'm a PHP newbie and I'm stumped on a piece of making a form submit.

    A little background:
    - I am trying to make the if(isset($_POST)) dynamic since I will have many pages that have the same basic function but will place data into different tables/fields.
    - I am able to get the form to submit correctly if I don't use any variables, and hard code something like: $name1 = $_POST['name1'];

    The process works like this:
    1) A form is created that automatically is given a name. For example:
    Code:
    <div class='form-group'><textarea class='form-control' rows='5' name='{$name1}'>{$sampleData}</textarea></div>.
    I should note that according to the HTML source code, there is no issue assigning the correct name and the form looks correct. I will also note that with any given form, there maybe 1 or 5 ...or some other number of fields.
    2) I send the information via post and this is where it is getting tricky. What I have is below:
    Code:
    if(isset($_POST['submit'])){
    $name1 = $_POST['name1'];
    $name2 = $_POST['name2'];
    $name3 = $_POST['name3'];
    $nextPage = $_POST['nextpage'];
    $date = date('Y-m-d H:i:s');
    $userid = 10;
    
    	if(isset($_POST['submit'])) {
    	header("Location: /pages/$nextPage"); 
    	$query = "INSERT INTO tablename (id, field1,field2, field3, lotdrivewaydate) ";
    	$query .= "VALUES ($userid, '$name1', '$name1', '$name2', '$name3') ";
        $result = mysqli_query($connection, $query);
        echo "$nextPage";
    	if(!$result) {
        die("QUERY FAILED" . mysqli_error($connection));    
       }
    	}
    }
    So you will first notice that $name1 = $_POST['name1']; and the column names are hard coded. Is there a good way to pull my automatically generated form names into $_POST? How about automatically pulling in the column names?

    I've been working on this for quite a while and haven't been able to crack it. Hopefully, this is clear but let me know if I can provide more detail. Thank you in advance for any ideas you all have!

  2. #2
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    You would write or find (this is not the first time anyone has done this) a 'form generator/form processor' script.

    You would define the dynamic information about your form (each field name, label, field type, field parameters, css class for styling, if existing data should populate the field, and any other things you can think of...) and form processing code (if a value is required or not, data type - type of validation, and any other things you can think of...) somewhere (a database table, php array), then use that information to dynamically produce the form (which it sounds like you are already doing) and dynamically process the submitted form data.

    Instead of the hard-coded logic you have posted to processes the form data, you would loop over the definition you have created and validate each field based on the definition. At the end, if there are no validation errors, you would use the submitted form data.

    For the database query(ies) that use the data, after you have validated it, you would do something similar, define the dynamic information needed to build the queries (table name, field names that are permitted for each type of query, what to use as the data source for each field, ...), to make a general purpose CRUD (Create - Insert, Read - Select, Update, Delete) application.

Similar Threads

  1. Replies: 1
    Last Post: 09-23-2012, 02:46 AM
  2. Replies: 0
    Last Post: 04-11-2009, 08:48 AM
  3. Replies: 1
    Last Post: 08-19-2008, 01:36 AM
  4. Replies: 3
    Last Post: 08-06-2008, 02:17 AM
  5. Replies: 2
    Last Post: 11-19-2007, 07:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •