Using PHP with WFB form in RSD - Post...

User 2778933 Photo


Registered User
16 posts

Hi there!

I am hoping someone might be able to help me or point me in the right direction.

I am trying to move data in and out of my form. I created my form in Web Form Builder (WFB). Then, I put that form in RSD - in an HTML element. So the form is now in my RSD page. But my challenge now is using PHP to bring in data to the form, and also save the new data in the form. I'm putting it in and out of a database on my web host.

But I do NOT know how to do this. Could someone help me or point me in the right direction? I'm trying to figure out how to add the PHP to the WFB form (that is inserted into an RSD page) in order to make the form accept and save data (to a database). What file do I access?

Thank you in advance!!
User 187934 Photo


Senior Advisor
20,190 posts
Online Now

I can help you with this but I need more information.
Do you have your form working and submitting to a database?
What information do you want to fill in on the form and how do you want to determine the information?
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2778933 Photo


Registered User
16 posts

Yes, I have a form.

Here is an example. This is the code for the WFB form. I made a change by adding the '$Lastname' code. Earlier in the code, there is a SQL statement that populates the $LastName variable. Later, it will save the content and update in the database. I use PHP.
<div class='fb-input-box'>
<input type='text' id='item21_text_1' maxlength='50' placeholder='' autocomplete='off'
data-hint='' name='LastName' required value=' . $LastName . '>
</div>


Here is a very close example of what the form would look like:
http://mayvista.com/dashboard_1.html

My issue is:
WFB creates the form, but I need to go in and change it (add these variables and the PHP). My questions are....
1) Where do I find the file to make these changes? I use the automated folder structure that WFB creates when I export.
2) Is there an >easy way< to add these PHP changes to the form? What do other developers do?
3) Do I have to redo the file every time I export a form in WFB? That is, if I update the form just a little (like add a new question), do I have to redo all the other form elements since the WFB export over-writes the previous file.

I hope this makes sense! I'm happy to give more details. I'll be using different types of WFB forms (often) with PHP and SQL - to move data in and out of a database. So I'd like to learn the easiest way of doing it. Thank you!!

Warm regards :)



What information do you want to fill in on the form and how do you want to determine the information?
User 187934 Photo


Senior Advisor
20,190 posts
Online Now

Here's the code of a form that I did as an example. My form has 6 fields named field1, field2, field3, field4, field5, field6. I have one text, number, dropdown, email, checkbox and radio element. This form is using a get variable to grab the email to pull the data from a table named form_fields in the database to populate the form fields. That would appear in the url as myform.html?var4=myemail@mydomain.com. The only thing I didn't do is use some kind of lookup to check the database for the email already existing and not letting the user submit it. Ajax might be a great option for that.
Altering forms takes a while but once you do it it actually goes pretty fast. I have my server setup to parse php within html.
<?php

// Lets query if variables are set
if(isset($_GET['var4'])){
$var4 = $_GET['var4'];



// Connect to your database
$database = 'mysql: myHost =localhost;dbname=enter-database-name-here;charset=utf8';
$username = 'enter-username-here';
$password = 'enter-password-here';
try {
$pdo = new PDO($database, $username, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo ->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

// Query the database table
$query = $pdo->prepare("SELECT field1,field2,field3,field4,field5,field6 FROM form_fields WHERE field4 = :field4");
$data = array(
':field4' => $var4
);
$query->execute($data);
$row = $query -> fetch();
// set the field name variables
$field1 = $row['field1'];
$field2 = $row['field2'];
$field3 = $row['field3'];
$field4 = $row['field4'];
$field5 = $row['field5'];
$field6 = $row['field6'];
//separate the checkbox array Check 1, Check 2, Check 3
$checked = explode(', ',$field5);
// set a variable for each of the 3 possible checkbox options
$fielda = $checked[0];
$fieldb = $checked[1];
$fieldc = $checked[2];

}
?>
<!DOCTYPE HTML>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Start of the headers for CoffeeCup Web Form Builder -->
<meta name="viewport" content="width=device-width, initial-scale=1.0"
/>
<script type="text/javascript" src="common/js/form_init.js" data-name=""
id="form_init_script">
</script>
<link rel="stylesheet" type="text/css" href="theme/default/css/default.css"
id="theme" />
<!-- End of the headers for CoffeeCup Web Form Builder -->
<title>
data-base-form
</title>
</head>

<body><!-- Start of the body content for CoffeeCup Web Form Builder -->
<style>#docContainer .fb_cond_applied{ display:none; }</style><noscript><style>#docContainer .fb_cond_applied{ display:inline-block; }</style></noscript><form class="fb-toplabel fb-100-item-column selected-object" id="docContainer"
action="../data-base-form.php" enctype="multipart/form-data" method="POST"
novalidate="novalidate" data-form="manual_iframe">
<div class="fb-form-header" id="fb-form-header1">
<a class="fb-link-logo" id="fb-link-logo1" style="max-width: 104px;" target="_blank"><img title="Alternative text" class="fb-logo" id="fb-logo1" style="width: 100%; display: none;" alt="Alternative text" src="common/images/image_default.png"/></a>
</div>

<div class="section" id="section1">
<div class="column ui-sortable" id="column1">
<div id="fb_confirm_inline" style="display: none; min-height: 200px;">
</div>
<div id="fb_error_report" style="display: none;">
</div>
<div class="fb-item fb-100-item-column" id="item1" style="opacity: 1;">
<div class="fb-header">
<h2 style="display: inline;">
Mysql form Fields
</h2>
</div>
</div>
<div class="fb-item fb-100-item-column" id="item2">
<div class="fb-grouplabel">
<label id="item2_label_0" style="display: inline;">Number Field 1</label>
</div>
<div class="fb-input-number">
<input name="field1" id="item2_number_1" required type="number" min="0"
max="999999999" step="1" autocomplete="off" data-hint="" value="<?php if(isset($field1)){echo $field1 ;} ?>" />
</div>
</div>
<div class="fb-item fb-100-item-column" id="item7">
<div class="fb-grouplabel">
<label id="item7_label_0" style="display: inline;">Text Field 2</label>
</div>
<div class="fb-input-box">
<input name="field2" id="item7_text_1" type="text" maxlength="254" placeholder=""
autocomplete="off" data-hint="" value="<?php if(isset($field2)){echo $field2 ;} ?>" />
</div>
</div>
<div class="fb-item fb-100-item-column" id="item3">
<div class="fb-grouplabel">
<label id="item3_label_0" style="display: inline;">Select an option Field 3</label>
</div>
<div class="fb-dropdown">
<select name="field3" id="item3_select_1" required data-hint="">
value="<?php if(isset($field3)){echo '<option selected >'.$field3.'</option>';} else{echo '<option id="item3_0_option" selected value="">
Choose one
</option>';} ?>"

<option id="item3_1_option" value="Option 1">
Option 1
</option>
<option id="item3_2_option" value="Option 2">
Option 2
</option>
<option id="item3_3_option" value="Option 3">
Option 3
</option>
</select>
</div>
</div>
<div class="fb-item fb-100-item-column" id="item6">
<div class="fb-grouplabel">
<label id="item6_label_0" style="display: inline;">Email Field 4</label>
</div>
<div class="fb-input-box">
<input name="field4" id="item6_email_1" required type="email" maxlength="254"
placeholder="you@domain.com" autocomplete="off" data-hint="" value="<?php if(isset($field4)){echo $field4;} ?>" />
</div>
</div>
<div class="fb-item fb-three-column fb-100-item-column" id="item8">
<div class="fb-grouplabel">
<label id="item8_label_0" style="display: inline;">Check options Field 5</label>
</div>
<div class="fb-checkbox">
<label id="item8_0_label"><input name="field5[]" id="item8_0_checkbox" type="checkbox" data-hint="" value="Check 1" <?php if ($fielda == "Check 1" )
{echo 'checked = "checked"';}?> /><span class="fb-fieldlabel" id="item8_0_span">Check 1</span></label>
<label id="item8_1_label"><input name="field5[]" id="item8_1_checkbox" type="checkbox" value="Check 2" <?php if ($fielda == "Check 2" || $fieldb == "Check 2" )
{echo 'checked = "checked"';}?> /><span class="fb-fieldlabel" id="item8_1_span">Check 2</span></label>
<label id="item8_2_label"><input name="field5[]" id="item8_2_checkbox" type="checkbox" value="Check 3"<?php if ($fielda == "Check 3" || $fieldb == "Check 3"|| $fieldc == "Check 3" )
{echo 'checked = "checked"';}?> /><span class="fb-fieldlabel" id="item8_2_span">Check 3</span></label>
</div>
</div>
<div class="fb-item fb-three-column fb-100-item-column" id="item9">
<div class="fb-grouplabel">
<label id="item9_label_0" style="display: inline;">Select an option Field 6</label>
</div>
<div class="fb-radio">
<label id="item9_0_label"><input name="field6" id="item9_0_radio" type="radio" <?php if ($field6 == "Radio 1" )
{echo 'checked = "checked"';}?> data-hint="" value="Radio 1" /><span class="fb-fieldlabel" id="item9_0_span">Radio 1</span></label>
<label id="item9_1_label"><input name="field6" id="item9_1_radio" type="radio" <?php if ($field6 == "Radio 2" )
{echo 'checked = "checked"';}?> value="Radio 2" /><span class="fb-fieldlabel" id="item9_1_span">Radio 2</span></label>
<label id="item9_2_label"><input name="field6" id="item9_2_radio" type="radio" <?php if ($field6 == "Radio 3" )
{echo 'checked = "checked"';}?> value="Radio 3" /><span class="fb-fieldlabel" id="item9_2_span">Radio 3</span></label>
</div>
</div>
</div>
</div>
<div class="fb-item-alignment-left fb-footer" id="fb-submit-button-div"
style="min-height: 1px;">
<input class="fb-button-special" id="fb-submit-button" type="submit" data-regular=""
value="Submit" />
</div>
<input name="fb_form_custom_html" type="hidden" />
<input name="fb_form_embedded" type="hidden" />
<input name="fb_js_enable" id="fb_js_enable" type="hidden" />
<input name="fb_url_embedded" id="fb_url_embedded" type="hidden" />
</form>
<!-- End of the body content for CoffeeCup Web Form Builder -->


</body>
</html>
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 187934 Photo


Senior Advisor
20,190 posts
Online Now

I had a few minutes and did a little more tweaking to the form above. I'm now checking the data base for the email and I'm using the $_SESSION variables the form builder uses to bring the user back to the form and fill it in with what they submitted.
You can see it working here.
http://ericrohloff.com/coffeecup/ccforu … index.html
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2778933 Photo


Registered User
16 posts

This is really helpful. Thank you!!
I'm going to go through everything and try to make it work with my code. So excited to have some input!!
User 187934 Photo


Senior Advisor
20,190 posts
Online Now

Let me know if you need help. I'm still not totally sure on what your needs were on how to get the data. Populating the data into the fields is the simpler part. Which data and how it actually gets there is the part that will always be user specific. Here's my formbuilder fb file and and all supporting files as I used a php include for the database connection and a php file for the json data to post to so it could check the email address after it was entered.
My finished form is included so you can see the alterations I made.
http://ericrohloff.com/coffeecup/ccforu … -files.zip
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2736027 Photo


Registered User
68 posts

Hi Eric,

Your form looks like something I could use as well. Thanks for putting it out there!

Before I modify it for my own application, I just need to better understand how to actually implement it. I tried the link above to your form, and entered some details, along with a dummy email address. I submitted the form. Next, I attempted to include this email address in the URL, to see if it would recognize it and retrieve the stored data, but it did not load the previously entered fields into the form. All I saw was a blank form, again.

So, question... is your sample form actually saving form submissions, or have I not entered the URL correctly so that var4 is passed on to the php query? I tried this format: http://ericrohloff.com/coffeecup/ccforu … domain.com

eric@somedomain.com is the email address I used the first time I used the form, which should have been saved, no?

Let me know how I may have mis-read your instructions.

Thanks!
User 187934 Photo


Senior Advisor
20,190 posts
Online Now

I switched the form to use the session variables instead of the url variables. The post that has all the code listed is everything you need to use url variable but that doesn't check to see if the email is already in the database.
Norm try using the same email again in my example form.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2736027 Photo


Registered User
68 posts

Partial success!!

If I use the code posted about 6 messages above, then YES, it does work as described in that block.

If I use the newer code available in the zip file where you now use session variables, then I am confused: I fill in the form for the first time, and then after I hit 'submit' I am returned back to the same form, but it is now filled in. It does not retrieve data from a URL request, as the original code does. So, there must be new/different index page, or other form, that the session data comes from? Would it be from a log-in page, for instance.

I'm almost there, but your first example code would probably work well enough for me.

Thanks.

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.