This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Dynamic content (PHP)


  1. closer
    Member

    Hello,

    Is there a way to add dynamic content to the form using php?

    like view.php?id=1&title=manhunt

    fills the title field automatically with manhunt

    Cheers

    Posted 16 years ago #
  2. closer
    Member

    After some playing around I found how to do it by editing the view-functions.php

    thanks anyway!

    Posted 16 years ago #
  3. yuniar

    Oh wow, I'm actually doing some testing and going to write the response for you.
    But it seem you are doing it faster than me.

    Great!
    Let me know if you need any help.


    MachForm Founder

    Posted 16 years ago #
  4. sk
    Member

    can you post how you did this? would like to know as well

    Posted 16 years ago #
  5. yuniar

    Hi sk,

    I'll try to help you with this.

    Let's take a sample with this form:
    http://www.appnitro.com/demo/view.php?id=5

    Let say our goal is to populate the email field, so the url would be like this:
    http://www.appnitro.com/demo/view.php?id=5&email=test@example.com

    Now, our form_id is 5 and the email input field is having id element_3 (view source to see it).

    So, what you need to do is, edit your view-functions.php file, on line 1682 you will find this code:

    $element[$j]->default_value = htmlspecialchars($row['element_default_value']);

    Right below that line, add this code:

    if($form_id == 5 && $row['element_id'] == 3){
    	$element[$j]->default_value = $_GET['email'];
    }


    Save the file and you should be all set.
    Of course you will need to adjust those form id and element id number.

    I hope I'm being clear for you. Feel free to post any questions.


    MachForm Founder

    Posted 16 years ago #
  6. bkonia
    Member

    Here's a more universal way of doing it:

    $title = str_replace(' ', '_', $element[$j]->title);
    if (isset($_GET[$title])) $element[$j]->default_value = $_GET[$title];

    Add those two lines after line 1682. Now, any parameter that you pass in the query string will automatically populate any field that has a title matching the query string parameter name. If your field title contains spaces, you can pass the query string parameter using underscores in place of the spaces and it will work fine.

    Posted 16 years ago #
  7. jcs53
    Member

    How can I pass a variable from the current form to a second form by using the redirect url in the form properties. I could run a php program that sets the variable, but where do i get the value for a particular form field.

    Thanks

    Posted 16 years ago #
  8. yuniar

    Hmm.. this one might be a bit more complex.

    You will need to modify one file includes/post-functions.php

    Inside process_form() function, you need to modify $process_result['form_redirect'] variable. Append any necessary value to this variable to pass it to the redirected form.

    To get the value for a particular form field, you can use $table_data['xxxx'] variable.
    Replace 'xxxx' with your field id. For example: $table_data['element_2']

    If this sounds too complex, please mail me directly. I'll try to help you there.


    MachForm Founder

    Posted 16 years ago #
  9. jcs53
    Member

    Thanks yuniar. I am now able to capture the data i need to pass to the next form. However I am trying to use the universal way of passing the data to the new form mentioned by bkonia above. I'm not sure what to use for the title in his example when passing the data ie title=abc. Is title the field label on the receiving form?

    Posted 16 years ago #
  10. jcs53
    Member

    Regarding last post, I have it figured out using your code yuniar, can't seem to make it work with universal method, but thats ok for what I need to do. I believe I can successfully handle a multipage form. Thanks

    Posted 16 years ago #
  11. knitzsche
    Member

    I'd like to do this for a name field where I want to set the first name & last name. Because the field is not a single element field, I can't figure out the syntax. How do I do this?

    I first tried setting them in the display_name method. Then I see here that a default value can be set in the display_form method.

    Thanks

    Kalpana

    Posted 15 years ago #
  12. yuniar

    You need to modify display_simple_name function and set the default value for those fields.

    For example, put this code into the beginning section of display_simple_name() function:

    if($_GET['id'] == 5){
    
    	if(!empty($_GET['first'])){
    		$element->populated_value['element_'.$element->id.'_1']['default_value'] = trim($_GET['first']);
    	}
    
    	if(!empty($_GET['last'])){
    		$element->populated_value['element_'.$element->id.'_2']['default_value'] = trim($_GET['last']);
    	}
    }


    Then you can populate those fields by using this URL:
    http://www.appnitro.com/demo/view.php?id=5&first=John&last=Doe


    MachForm Founder

    Posted 15 years ago #
  13. andykinsey
    Member

    I'm looking for the same but for a text area, or single line text.... though if the later the smae fields later in the form must be default clear?

    Posted 15 years ago #
  14. redityo

    Hi,

    It almost same with above code to use in single line or text area. For single line text you can add this code around line 39~40 :

    if ($_GET['id'] == '1' && $element->id == '1') {
    	if (!empty($_GET['text_area']))
    		$element->default_value = $_GET['single_text'];
    }

    and for text area, you can add these code around line 97 ~ 88:

    if ($_GET['id'] == '1' && $element->id == '4') {
    	if (!empty($_GET['text_area']))
    		$element->default_value = $_GET['text_area'];
    }

    In that code, I assume you have form id = 1, single text element id = 1 and text area element id = 4


    MachForm Support

    Posted 15 years ago #
  15. purplelemongraphics
    Member

    I am also trying to get name field to auto populate in linked forms...

    It is for a camp online registration form - the name of the camper on the first form needs to carry over to the next page... (ultimately - I would like the name field, email address, a drop down menu and a check box to auto populate but I'm shooting for something that can tie each section of the form together so registrants wont' have to re-key information).

    I tried to add the code put the code into the beginning section of display_simple_name() function and it didn't work. Should it be "if($_GET['form_id'] == 5)"?

    Is there any way that, when redirecting forms, a universal code can be entered so you wouldn't need to clarify each form?

    Posted 15 years ago #
  16. redityo

    it's not always "if($_GET['form_id'] == 5)", it depend on your form id number. For example your form URL is like this :

    http://www.yourdomain.com/machform/view.php?id=2

    That indicate your form id = 2, so you should adjust this :

    if($_GET['form_id'] == 5)

    to

    if($_GET['form_id'] == 2)

    anyway I'm not sure with a universal code, do want to redirect a page after submission along with form data ?

    try to look at this post :

    http://www.appnitro.com/forums/topic/send-form-data-to-http-postget-url?replies=2


    MachForm Support

    Posted 15 years ago #
  17. purplelemongraphics
    Member

    I know that I have to alter the number to the id of the form I want to pull the data from. .. what I wasn't sure about was if it is:

    "if($_GET['form_id'] == 5)"

    or

    "if($_GET['id'] == 5)"

    Coincidently - my form that is the main page IS 5... so that number works for what I am using it...

    BUT - Then... to have the Name auto populate on the redirected form - taking the name from form "5" to the form it is redirected to - "4"... how do I alter this additional information? The main form I am pulling info from is "5" - the Name Field I am wanting to populate is "element_1"... do I put the code below in as it is...

    "if(!empty($_GET['first'])){
    $element->populated_value['element_'.$element->id.'_1']['default_value'] = trim($_GET['first']);
    }

    if(!empty($_GET['last'])){
    $element->populated_value['element_'.$element->id.'_2']['default_value'] = trim($_GET['last']);"

    or do I adjust

    "['element_'.$element->id.'_1']" to be "['element_1'.$element->id.'_1']"?

    I'm confused... (and tired)... this is the URL for my form...

    "https://www.butterfieldacres.com/_machforms/view.php?id=5"

    It is a summer camp in which the first section is the registration info, the second is for a Snack Option, third is for Lunch Option, etc. - so form id=5 -- links to id=4 -- that links to id=3 and so forth...

    Everything works awesome in MachForms... I just have to eliminate the need for parents to have to re-enter their child's data each new section - but still have a common field on the redirected form that links the person requesting the option to the child that is registered for it...

    Does that make any sense? I need - at the least - the child's name and the parent's email address to auto populate on each redirected form... there are at least 5. I need the child's name to know who is registered for the service and the email address to receive the confirmation of their registration.

    How in the world do I do this in the easiest steps possible...??? I'm exhausted from over thinking it...

    HELP! Ugh.

    Posted 15 years ago #
  18. redityo

    Hi,

    Sorry.. I missed that, you should use if($_GET['id'] == 5) to get form ID. Anyway from your explanation I think you want to get data from previous form,looks like multipage.
    I think You will do much modification for this, I'll try to populate child's name and email form data with this form link --> form 3 -- form 2 -- form 1 . in this example I use this form details information :

    1. form 1 (id == 1)
    -name field : element 1
    -email field : element 2
    2. form 2 (id == 2)
    - name field : element 3
    - email field : element 2
    3. form 3 (id == 3)
    - name field : element 1
    - email field : element 2

    Make sure you adjust form id and element id with yours, I think you have different element id for each form.

    try to follow these steps (i hope this will clear enough :) ) :

    1. Edit post-functions.php, goto around line 940~951 you'll find these code :

    foreach ($table_data as $key=>$value){
    
    	if($value == ''){ //don't insert blank entry
    		continue;
    	}
    	$value		    = mysql_real_escape_string($value);
    	$field_list    .= "<code>$key</code>,";
    	$field_values  .= "'$value',";
    
    	if(!empty($value)){
    		$has_value = true;
    	}
    }

    replace with this

    foreach ($table_data as $key=>$value){
    
    	if($value == ''){ //don't insert blank entry
    		continue;
    	}
    	$value		    = mysql_real_escape_string($value);
    	$field_list    .= "<code>$key</code>,";
    	$field_values  .= "'$value',";
    
    	if(!empty($value)){
    		$has_value = true;
    	}
    
    //set redirect
    
    if ($form_id == '3') {
    	//set for form 2
    	switch ($key) {
    		case 'element_1_1' :
    			$populate_data .= 'first=' . $value . '&';
    			break;
    		case 'element_1_2' :
    			$populate_data .= 'last=' . $value . '&';
    			break;
    		case 'element_2' :
    			$populate_data .= 'mail=' . $value . '&';
    			break;
    	}
    
    } elseif ($form_id == '2') {
    	//set for form 2
    	switch ($key) {
    		case 'element_3_1' :
    			$populate_data .= 'first=' . $value . '&';
    			break;
    		case 'element_3_2' :
    			$populate_data .= 'last=' . $value . '&';
    			break;
    		case 'element_2' :
    			$populate_data .= 'mail=' . $value . '&';
    			break;
    	}
    }
    }
    
    if (substr($populate_data ,0,-1) != ''){
    if ($form_id == '3'){
    	//redirect to form 1
    	$process_result['form_redirect']  = "http://yourdomain.com/machform/view.php?id=2&" . substr($populate_data ,0,-1) ;
    } elseif ($form_id == '2') {
    	//redirect to form 1
    	$process_result['form_redirect']  = "http://yourdomain.com/machform/view.php?id=1&" . substr($populate_data ,0,-1) ;
    }
    }

    2. Edit view-functions.php, go to line 308 and put these code :

    if ($_GET['id'] == '2' && $element->id == '2') {
    	//populate data from form 3
    	if (!empty($_GET['mail'])) {
    		$element->default_value = $_GET['mail'];
    	}
    } elseif ($_GET['id'] == '1' && $element->id == '2') {
    	//populate data from form 2
    	if (!empty($_GET['mail'])) {
    		$element->default_value = $_GET['mail'];
    	}
    }

    exactly above this code :

    $element_markup = <<<EOT

    3. Edit view-functions.php, goto line 700 and put these code :

    if ($_GET['id'] == '2' && $element->id == '3') {
    	//populate data from form 3
    	if (!empty($_GET['first']))
    		$element->populated_value['element_'.$element->id.'_1']['default_value'] = $_GET['first'];
    
    	if (!empty($_GET['last']))
    		$element->populated_value['element_'.$element->id.'_2']['default_value'] = $_GET['last'];
    
    } elseif ($_GET['id'] == '1' && $element->id == '1') {
    	//populate data from form 2
    	if (!empty($_GET['first']))
    		$element->populated_value['element_'.$element->id.'_1']['default_value'] = $_GET['first'];
    
    	if (!empty($_GET['last']))
    		$element->populated_value['element_'.$element->id.'_2']['default_value'] = $_GET['last'];
    
    }

    MachForm Support

    Posted 15 years ago #
  19. aarenhofferth
    Member

    I have tried both the solutions by "yuniar" and "bkonia" and neither have worked. Can you outline what steps you took? I have added the code above to no avail.

    Posted 15 years ago #
  20. yuniar

    aaren, can you send us your modified file? we'll check it.

    Also, let us know the URL to your form and which fields would you like to populate.
    Send to: customer.service [at] appnitro.com


    MachForm Founder

    Posted 15 years ago #

RSS feed for this topic

Reply »