I need some guidance on how to properly code a dynamic web page containing 10 questions each having its own submit button. Here's a quick background and what I'm doing.
I'm creating a question/answer page in which a user answers questions and hits Submit. Each question has a point value which gets added to the user's bank if answered correctly.
The questions are populated on the page through a database table containing three fields - QuestionNumber, Question, Points. Each Question has a QuestionNumber assigned to it so I can easily and quickly ID the question and answer. This displays fine (here's the code):
<?
$rt = mysql_query("SELECT * FROM Questions WHERE Status='Open' ORDER BY 'Number' LIMIT 10");
while ($row = mysql_fetch_array($rt)) {
$number=$row[0];
$category = $row[1];
$question=$row[2];
$points=$row[4];
?>
<form action="processor.php" method="post" class="qForm">
<div class="questionCell">
<div class="question"><? echo $question; ?></div>
<div class="answer">Answer: <input class="inputField" name="a0001" type="text" size="40" maxlength="40" />
<input name="HHQuestion" value="Question: <? echo $number; ?>" type="submit" /></div>
</div>
</form>
<?
}
?>
This form gets repeated 10 times on the page, and each has its own question and QuestionNumber. Also, every submit button has the value assigned based on the questionNumber. I did this so that the user (young kids) knows which question they're about to answer. This all displays very nicely on the page.
The problem I'm having is with the POST file "processor.php", which has not been created yet. The way I envision it to work is that when someone submits their answer, the processor.php file will capture the quesiotnNumber (that's assigned to the button) and answer from the text field, then validate it again the database table.
I know that I should be using SWITCH / CASE (as opposed to IF ELSE) when using multiple buttons, which I'm fine with. But how do I capture the QuestionNumber on the form into the processor.php file?
My ultimate goal for this project is to just populate the question table in the database, and let the webpage work dynamically.
Thanks in advance for any help and guidance.
R
I'm a little confused about your wording, but am I understanding correctly in thinking that you wish to submit the answer to each question to the database although you have them all running on the same page?
If so, I'd recommend using an AJAX library to send the data back and get a quick response. There are a number out there, but jQuery is very simple to use (along with having a number of other features). Below is some psuedocode:
function submitAnswer(questionid)
{
answer = document.getElementById("a000"+questionid);
$.get("processor.php", {"answer": answer, "question": questionid}, function (data) {
if (data.response == "200") {
document.getElementById("response-"+questionid).innerHTML = "Thanks for answering";
}
});
}
Then you could call it with something like:
<input type = "submit" onclick = "submitAnswer('<?=$number?>'); void(0); return false;">
Please note this is all psuedo! Your mileage may vary! Shout out if you have any questions.
Related
I'm a little frustrated with Gravity Forms at the moment.
Here's my setup:
I have a custom registration form created with WPEverest's User Registration plugin. This plugin creates a custom user registration form that seamlessly integrates with WooCommerce, and also saves custom fields to the USER, rather than in a separate table.
I'm trying to use Gravity Forms to allow users to update field's created with WPEverest's Registration Plugin(which at this point is just some custom user meta fields), but I'm having a hell of a time getting the field values.
My biggest issue is that I can't for the life of me figure out what the heck value I'm supposed to pass through get_post_custom_values();.
The code below is what I have so far. This is supposed to pass a value to the user meta field upon form submit, and it does work... but only if I set the value as a manually created variable. It will not work if I try to pull the data from Gravity Forms.
$gravity_form_id = '10'; // Gravity Forms ID
add_action("gform_after_submission_$gravity_form_id", "gravity_post_submission", 10, 2);
function gravity_post_submission ($entry, $form){
$post_id = $entry["post_id"];
//trying to get values from gravity form
$values = get_post_custom_values("field_name???", $post_id);
//updating user meta
update_post_meta($current_user->ID, 'user_registration_body_type', $values);
}
For reference, the form object I'm using is a RADIO type with ID 10, and a parameter name of body_type (I'm using this name as a means to pre-populate the form fields).
Here's how the form field renders:
<div class="ginput_container ginput_container_radio">
<ul class="gfield_radio" id="input_14_10">
<li class="gchoice_14_10_0">
<input name="input_10" type="radio" value="apple" id="choice_14_10_0">
<label for="choice_14_10_0" id="label_14_10_0">Apple</label>
</li>
<li class="gchoice_14_10_1">
<input name="input_10" type="radio" value="full-bust" id="choice_14_10_1">
<label for="choice_14_10_1" id="label_14_10_1">Full Bust</label>
</li>
<li class="gchoice_14_10_2">
<input name="input_10" type="radio" value="hourglass" checked="checked" id="choice_14_10_2">
<label for="choice_14_10_2" id="label_14_10_2">Hourglass</label>
</li>
<li class="gchoice_14_10_3">
<input name="input_10" type="radio" value="pear" id="choice_14_10_3">
<label for="choice_14_10_3" id="label_14_10_3">Pear</label>
</li>
<li class="gchoice_14_10_4">
<input name="input_10" type="radio" value="straight" id="choice_14_10_4">
<label for="choice_14_10_4" id="label_14_10_4">Straight</label>
</li>
</ul>
</div>
TL;DR - What am I supposed to replace form_field??? with in order to retrieve the form field value? What ID or Name am I supposed to use? (ex: input_10, input_14_10, body_type ?) I'll be trying to do the same thing with checkboxes later, but it seemed easier to start with a radio button since it only returns one value.
Or am I approaching this problem all wrong?
Thanks in advance for your help!
Try something like this.
Add form id to the gform_after_submission_. It looks like you are using form id 14 but this code are for form id 10.
use rgar to specify the field id (10)
use update_user_meta
Get the current user ID (This means that you must be logged in before subitting the form.
add_action("gform_after_submission_10", "gravity_post_submission", 10, 2);
function gravity_post_submission ($entry, $form){
//Gets field id 10
$values = rgar( $entry, '10' );
//updating user meta
update_user_meta( get_current_user_id(), 'user_registration_body_type', $values );
}
This is an alternative answer. I know my original question was asking about the ID number, but after a lot of experimentation, I learned that there's no need for this code at all (which addresses the second part of my question, "am I approaching this issue wrong?").
Gravity forms actually has the in-built ability to write data to custom fields (even for users through a registration process), but does not have the ability to create custom fields itself. I finally found this information in the Gravity Forms Documentation.
Doing so is very simple, but you have to have the PRO version.
To do this:
Create a Custom Field for the post type (or in the example's case, the user meta) - you can use something like Advanced Custom Fields for this, or custom code in your functions.php file
Use the Post Custom Field form item from the Advanced form item options in the form builder
From the Post Custom Field's Custom Field Name section, select "Existing" and then the name of the field you created from the dropdown.
Gravity Forms handles the rest.
So while Fredrik's answer is correct, if you are able to create a custom field, you can just use Gravity Forms existing functionality and avoid adding extra code altogether.
Hope this helps someone as the gravity_post_submission hook is... finicky at best - at least in my experience.
Not a Wordpress guru. I have inherited a small simple plugin that allows a user to upload an image, which then gets added to an array, and then you can add / edit array values for that image (title, description, etc). The issue I am having is it works great for awhile, then suddenly the form stops saving values to the option_value in the db. This could occur after 10-15 entries. I have verified that the payload being sent to options.php is correct.
Why does this plugin randomly stop saving data to the table?
I have registered the options array
$djwp_images = get_option('djwp_images');
add_action('admin_init', 'djwp_register_settings');
function djwp_register_settings() {
register_setting('djwp_images', 'djwp_images');
register_setting('djwp_settings', 'djwp_settings');
}
A single example shown below of how it iterates through the array for the form.
<form class="djform" method="post" action="options.php">
<?php foreach ($djwp_images as $id => $data) { ?>
// Single example shows how and array is used to input values
<input type="hidden" name="djwp_images[<?= $id; ?>][file_url]"
value="<?= $file_url ?>"/>
<? } ?>
<?= submit_button(); ?>
</form>
Full source code in pastebin
Using wordpress: 4.9.8
The problem is not directly related to WP. The problem is that on each loop (in for) it outputs about 20 inputs. In sum, it outputs hundreds of inputs, and you have to increase the amount of input_vars on your server. Either insert this in top of your functions.php, it should solve:
ini_set('max_input_vars', 3000);
ini_set('post_max_size', '128M');
or update those values in your server's php.ini (and restart server)
I realize there are dozens of this type of question, but since I can't find in any of them the solution to this specific problem, it's unlikely a "This question has already been answered here" type addition will make much sense. And to anyone who's ever tried getting a real, specific, detailed answer on the WordPress.org forums, good luck!
I'm using the Store Locator Plus plugin. Had some back-end coding issues which I had to fix because getting the guy (or company) who wrote it to answer questions on his website forum is like pulling teeth (not that anybody who spends $300 on a plugin deserves support, but anyhoo...)
So, here is a chunk of code where I hard coded a submit button html. Then I worked with a custom style sheet, and the button actually does work and look nice. The problem is that on the html source, WordPress has sprinkled spurious <p> and </p> tags in the html I created.
For now, please don't dwell on how the button is made, which is a bit of a hack. There are reasons why I used a div tag within a button content, it's because without it the text in the straight button comes out really crappy, not vertically aligned correctly, can't control the font-family or font-size values. Those problems will be for another question, at another time.
Here is the function in the plugin where I created a custom button:
function create_DefaultSearchDiv_Submit() {
$this->slplus->debugMP('msg',__FUNCTION__);
if (get_option(SLPLUS_PREFIX.'_disable_search') == 0) {
// Find Button Is An Image
//
if ($this->slplus->settings->get_item('disable_find_image','0','_') === '0') {
$sl_theme_base=SLPLUS_UPLOADURL."/images";
$sl_theme_path=SLPLUS_UPLOADDIR."/images";
if (!file_exists($sl_theme_path."/search_button.png")) {
$sl_theme_base=SLPLUS_PLUGINURL."/images";
$sl_theme_path=SLPLUS_PLUGINDIR."/images";
}
$sub_img=$sl_theme_base."/search_button.png";
$mousedown=(file_exists($sl_theme_path."/search_button_down.png"))?
"onmousedown=\"this.src='$sl_theme_base/search_button_down.png'\" onmouseup=\"this.src='$sl_theme_base/search_button.png'\"" :
"";
$mouseover=(file_exists($sl_theme_path."/search_button_over.png"))?
"onmouseover=\"this.src='$sl_theme_base/search_button_over.png'\" onmouseout=\"this.src='$sl_theme_base/search_button.png'\"" :
"";
$button_style=(file_exists($sl_theme_path."/search_button.png"))?
"type='image' class='slp_ui_image_button' src='$sub_img' $mousedown $mouseover" :
"type='submit' class='slp_ui_button'";
// Find Button Image Is Disabled
//
} else {
$button_style = 'type="submit" class="slp_ui_button"';
}
// TODO: find_button_label get_option should move to Enhanced Search
$string = "<div id='radius_in_submit'><button type=\"submit\" id=\"addressSubmit\" class=\"slp_ui_button\"><div class=\"button-inner\" >Find A Store</div></button></div>";
return $string;
}
return '';
}
You can see that two or three lines from the bottom, just before the closing bracket of the function, I wrote a variable named $string with the string value being the html of the button, which is wrapped in a div with an id='radius_in_submit'.
Now, here is the html output of the button:
<div id='radius_in_submit'><button type="submit" id="addressSubmit" class="slp_ui_button"></p>
<div class="button-inner" >Find A Store</div>
<p></button></div>
</p>
</div>
You can see that the "p" tags totally don't belong there.
What have I tried...I have tried many variations on adding the remove_filter('the_content','wpautop'); in the theme's functions.php file, but that has never really worked for me on anything for some reason.
Here is the latest code I tried, inserted just before the closing ?> in my functions.php file:
function rm_wpautop($content) {
global $post;
// Remove the filter
remove_filter('the_content', 'wpautop');
return $content;
}
// Hook into the Plugin API
add_filter('the_content', 'rm_wpautop', 9);
...but it doesn't solve my problem. The button does work, I could leave it alone, but I can't stand those p tag insertions.
Development url where you can see it: store locator
In my wordpress website, i am using a gravity form to complete one online application process with about 10 to 12 pages of form filling. Once a user fills out all the process and make it to payment method page (which is the last before page), he sees display of details about payment method he has chosen in previous page.
In the previous page, i had 3 options to choose like credit card, check, and money order. Now selecting credit card, works fine and displays its details in the next page. But for the other two - check and money order, anything among these are selected, the next page displays a black details area. I need to display of details according to the selection.
This is mainly because of the Jquery issue in displaying content.
In this page, there seems to be code like the below to display the contents and to stop them from displaying.
in Plugins/gravity_forms/form_display.php
<div id='gform_page_{$form["id"]}_{$field["pageNumber"]}' class='gform_page{$custom_class}' {$style}>
<div class='gform_page_fields'>
<ul class='gform_fields {$form['labelPlacement']}'>";
and li like
$style = !empty($form) && !IS_ADMIN && RGFormsModel::is_field_hidden($form, $field, $field_values) ? "style='display:none;'" : "";
$field_id = IS_ADMIN || empty($form) ? "field_$id" : "field_" . $form["id"] . "_$id";
return "<li id='$field_id' class='$css_class' $style>" . self::get_field_content($field, $value, $force_frontend_label, $form == null ? 0 : $form["id"]) . "</li>";
}
when form current page came then it show that fields other fields are display :none
I need to know how these process of display:block and none works accordingly in this situation. Which file is causing these all actions?
Here
i faced one more issue
for few id it does not show fields
so then for this i coded Themes/function-application.php
add_filter('gform_pre_render_'.$form_id, 'populate_checkbox',10);
function populate_checkbox($form)
{
in this ....
my code
if($current_page==12){
require_once 'includes/application_form/payment.php';
?>
<script type="text/javascript">
alert('inside');
jQuery("#field_1_212").css("display","block");
jQuery("#field_1_129").css("display","block");
alert('after');
</script>
<?php
}
?>
here i find only first alert inside worked fine .. and then jquery, alert after are all not worked..
so i need to know which file i need to modify ??
Thanks in advance..
I have a client who has signed up for a sagepay account. His current website runs off wordpress 3.0 and currently doesn't have any sort of ecommerce functionality.
He's needing a button that lets users submit a deposit of £300 through sagepay (this amount never changes).
(Usually, I would suggest using paypal for something like this, but apparently due to the travel nature of his business, paypal won't let my client have a pro account)
I've looked at the method described in a similar thread on here back in March (How Do I Make a SagePay BuyNow Button?), but I'm not really sure how to implement this within a page on wordpress, not hugely knowledgeable on php bar basic template editing, so I got totally lost at the $PAYMENT_CRYPT part.
If anyone could provide the steps I need to take to implement a basic button that submits the same amount each time, and then collects all the card details/customer details once it's sent them to sagepay gateway, it would be hugely appreciated!
In short, no. These is no easy way to approach this. Unless you link to a Payment form to SagePay and use the new IFRAME feature. You can have certain information within WordPress that allows PHP code on your template pages or your template files.
1 - IFRAME the form within your PHP server and code the form on its own that way the CSS will become like the CSS on the WordPress page
2 - Create a payment module for it
3 - Use an existing Payment eCommerce server module for WordPress - there are plenty of plugins already
4 - Create a payment button hyper link, once clicked, it goes to a PHP form on your server for the £300 amount..
5 - Use Nochex or another payment vendor, Google Wallet etc (this is not an easy option for the client)
With the FORM, you could have:
<?
# Define your vars
$serverLive="https://live.sagepay.com/gateway/service/vspform-register.vsp"
//$serverLive="https://test.sagepay.com/gateway/service/vspform-register.vsp"
$YOUR_VENDOR_LOGIN_NAME="";
$VendorTxCode="406227821909";
$Amount="350.00";
$Currency="GBP";
$Description="1 ACME Widget";
$SuccessURL="http://example.com/success.php";
$FailureURL="http://example.com/fail.php";
$BillingSurname="Smith";
$BillingFirstnames="John";
$BillingAddress1="123 Main Street";
$BillingCity="Anywhere";
$BillingPostCode="29555";
$BillingCountry="USA";
$DeliverySurname="Smith";
$DeliveryFirstnames="John";
$DeliverAddress1="123 Main Street";
$DeliveryCity="Anywhere";
$DeliveryPostCode="29555";
$DeliveryCountry="GBP";
# The address information can be done via jQuery on your page or get some defaults
?>
<form action="<?=$serverLive?>" method="POST" id="SagePayForm" name="SagePayForm">
<input type="hidden" name="VPSProtocol" value="2.23" />
<input type="hidden" name="TxType" value="PAYMENT" />
<input type="hidden" name="Vendor" value="<?= $YOUR_VENDOR_LOGIN_NAME ?>" />
<input type="hidden" name="Crypt" value="<?= $PAYMENT_CRYPT ?>">
<input type="image" src="images/buynow-sagepay.png" />
</form>
<script type="text/javascript">
function submitform()
{
document.SagePayForm.submit();
}
submitform();
</script>
Even with this code you would still need to use some SagePay libraries, such as the XOR and Crypt functions:
// Crypt and XOR functions
private function simpleXor($string, $password) {
$data=array();
for ($i=0; $i < utf8_strlen($password); $i++) {
$data[$i]=ord(substr($password, $i, 1));
}
$output='';
for ($i=0; $i < utf8_strlen($string); $i++) {
$output .= chr(ord(substr($string, $i, 1)) ^ ($data[$i % utf8_strlen($password)]));
}
return $output;
}