Store input from a text field in a PHP variable? - php

I'm fairly new to this, but I'm trying to costumize a plugin called democracy_poll for wordpress. It's a poll form where you can add your own answer. The answers get stored in the database, and what I need is to make the email input field I have created to output its value together with the answer (so I can contact the winner)
So like I said I have added an Email input field and would like to store the users input in a variable so I can see the input user email together with the answer they gave in the database.
I have added a new column in the database called author and added 'author' at the end of this code in the php file:
$exists = $wpdb->query(
$wpdb->prepare(
"SELECT aid FROM $wpdb->democracy_a " .
"WHERE answer = '%s' AND qid = $this->id",
$new_answer
)
);
if (!$exists)
if (
$wpdb->insert(
$wpdb->democracy_a,
array(
'qid' => $this->id,
'answer' => $new_answer,
'votes' => 0,
'added_by' => 1,
'author' => $author
)
)
)
with this, if
$author = "hello#email.com"
hello#email.com appears in the new column in the database next to the users answer.
So I'm wonder how to get the value from my email input box and store it in a php variable that I can use here?
My best guess so far has been $_POST["name"] but no luck :(
Thanks in advance!
W

You haven't posted any code of your HTML.
But, if you are using the value "name" in name attribute such as <input type="text" name="name"> then your $_POST["name"] will not work as "name" is a reserved name (or variable) for WP internal usage.
I suggest you to replace value in name attribute with something else like <input type="text" name="nameUnique"> and give it a go!

Thank you for your answers! We finally figured it out.
We had to define the variable for the email input in an ajax function,
then run a preg_match in the php file that handled the ajax data, against all the variables in the array to get the email address.
As I said I'm still new at this and it's very difficult for me to give you a detailed answer to my own question even though it was just solved.
I hope to keep learning and be able to contribute more in the future,
thanks again!!
W

Related

Contact Form 7 & Flamingo - Getting Flamingo Post ID as a Mail tag

On a WordPress website I'm using Contact Form 7 and Flamingo to manage contact forms, and store the data.
I've been using [_serial_number] in my emails to identify a submission which is stored in Flamingo. This serial number is added to a link which opens a page, and queries the database for the submission (by serial number) to display all the information online for the user who gets the email.
I've realised today that the serial number is reset for each form you create.
Eg. Form 1 submission serial numbers start at 1, and increments to 10 (for example).
If I then make Form 2, submission serial numbers start at 1 again.
This is causing a problem because there are multiple posts with the same serial number, so I'm not guaranteed to get the right submission.
I can't see a way of getting the Flamingo Post ID as a mail tag anywhere, I've looked through the code for Flamingo and can't see any hooks that would let me add in the ID of the post as a Mail Tag in CF7.
Does anyone know if this is possible?
I solved this issue by creating and storing my own unique token with each form submission and getting Flamingo posts by that via a meta_query instead of trying to get posts by the serial_number. Unfortunately it won't solve your problem for historical submissions that lack the custom token.
Create a field for the token in the CF7 Form tab. In my case I'm using CF7 Dynamic Text Extension to get a cookie value but it could even be static text. You just need a unique field that you can modify later.
[dynamichidden prtoken "DT_CF7_COOKIE key='dealer_id'"]
Generate the actual token value and replace the field content with it. I used the field value + unix time + a pseudo random blob so that I can see who generated it and when but it isn't guessable. My application is relatively low risk and I'm not a security expert.
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
if( !empty( $array['prtoken'] ) ) {
//Generate real token
$prtoken = $array['prtoken'] . '_' . time() . '_' . bin2hex(random_bytes(8));
if( !empty( $prtoken ) ) {
$array['prtoken'] = $prtoken;
}
}
return $array;
};
In my case the token forms part of a query string on a link in the email template:
Reply
Get posts matching the token. Should only ever be 0 or 1 but treat it as if it might match multiple flamingo items, just in case.
$args = array(
'post_type' => 'flamingo_inbound',
'fields' => 'ids', //Only return IDS
'meta_query' => array(
array(
'key' => '_field_prtoken', //The custom token field
'value' => sanitize_text_field($field_prtoken), //The token is input by users retrieving data so needs to be sanitized.
)
)
);
$postslist = get_posts( $args );

How to enumerate the forms in cf7?

I'm a newbie to web designing. I'm using contact form 7 to create a registration form for our conference.
All I wanted to do is, I need to give a unique id for all of them after they have registered for the conference and the further forms should be identified using this unique id.
So far, I have installed contact form 7 and contact form dtx
for this purpose and I have tried Koen de Bakker solution of generating a random number.
But it's slightly different from what I want, since it changes the random number for each refresh.
What I'd like is:
An unique number like "17ICLAA001,..." should be generated for each form submission.
Send the unique number to the applicant after successive submission of the form.(I hope this can be easily done once the shortcode is done).
Editing the form using the unique id.
Any help will be much appreciated. Thank you.
I have found a way to do this. Its just the number of rows+1 in the table.
When you add a record to your table the unique number also gets increased by 1 in the following code. Add the following function in function.php in your theme and use the short code "row_count" to call the function. Use it with dynamichidden text from dtx.
function row_count_shortcode() {
global $wpdb;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->username_wp1.SaveContactForm7_1" )+1;
return "17ICLAA".sprintf('%03d',$user_count);
}
add_shortcode( 'row_count', 'row_count_shortcode' );
Usually when you are creating contact forms using contact form7 it automatically creates a table in your database something like
username_wp1.SaveContactForm7_1
Instead of this replace your database table name.
So in your contact form, type
[dynamichidden uniqueid "row_count"]
and use [uniqueid] in your email body to serve your purpose.
It works fine. I have checked in my site.
Correct way to generate a unique and progressive number is to set a field in wp_option like this:
add_option('unique_number', '1');
When filter is called, you must simply increment this unique number:
function genTicketString() {
$currentUniqueNumber = get_option('unique_number');
$newCurrentUniqueNumber = $currentUniqueNumber + 1;
update_option('unique_number' $newCurrentUniqueNumber );
return $newCurrentUniqueNumber;
}
add_shortcode('quoteticket', 'genTicketString');

optional_param in form

I have a question about the optional_param function in a form in PHP (version 5.3.14)
After looking over why certain fields were not being saved in a form I have, I realised that this data...
$checkdata = optional_param('items', array(), PARAM_INT)
Only saves up to 996 places (items) from the form (they are select items and there are many)....
Is this a setting or a something I can change? or alternatively something wrong from my end?
Thanks in advance
Solution : A moodle function (platform i am working with)
Thanks Pekka
this function is a moodle function. It gets a parameter from the current page URL.
For an example url:
http://moodle.dev/course/view.php?id=2&items=4
(this is chosen totally arbitrary)
Using this code:
$checkdata = optional_param('items', array(), PARAM_INT)
Will save the "items" value (here it's 4) in $checkdata. If items does not exist in the url it will do $checkdata = array()

Default value not overridden upon form submission

I've got a hidden field inside a form, which I create using Drupal's form API like this:
$form['position'] = array(
'#type' => 'hidden',
'#default_value' => '57.149953,-2.104053',
'#attributes' => array(
'id' => 'geoposition',
)
);
When I load the page in which the form is rendered, I have a little bit of JavaScript that edits the hidden field like so:
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getPosition, noPosition, 10);
}
function getPosition(position) {
var pos = document.getElementsByName("position");
pos.value = position.coords.latitude + "," + position.coords.longitude;
alert(pos.value);
}
Now, the last statement in this snippet actually outputs the correct value of the user's current position. However, when I submit the form, the value passed onto the server for manipulation is the default value, and not the updated value. The value to use after the form has been submitted is picked up like this:
$map_field .= 'src="http://maps.google.co.uk/maps?q='.$form_state['values']['position'].'&output=embed"></iframe>';
Any ideas?
Thanks,
This is what I wud do:
1) Disable to js for now.
2) Use xdebug to see the value before and after the form has been submitted. This should help to see if something is going wrong.
Cheers,
vishal
Rather than using #default_value, consider using #value - because that is the value that gets posted.
Eventually solved it by using the getElementById() method instead of getElementsByName() method. For some reason the name wasn't working. Thanks all for your help.

Content of form element not being saved to database while others are

I am working with a CMS like system, phpBMS, which defines a certain way of making forms, and defines the form elements to be used in such forms.
This is an example of the form template, and these are the fields that are defined.
Generally, it is pretty simple. If you have an inputField with the id of say, 'name', the content of that field will be saved to the name field in the table the form is assigned to.
Currently, I am using a different input field, inputSmartSearch, which works a bit like google suggest as it can search and automatically display results as you type.
I want to use the content of this field to go into a 'product' table, but I am unsure of how to set this up.
I am calling my smartsearch like so:
$theinput = new inputSmartSearch($db, "chooseproducts", "Choose Product",$therecord["product"], "Choose Product", TRUE, NULL, NULL, TRUE, $required=true);
$theinput->setAttribute("class","important");
$theform->addField($theinput);
When I look what is returned by _POST, I see:
Array ( [chooseproducts] => 75c72a6a-83d9-11df-951a-fa9c1ec271f2 [ds-chooseproducts] => Corona [quantity] => 2 [type] => cash)
I have setup the quantity and type fields like so
$theinput = new inputField("quantity",$therecord["quantity"],"Quantity",true, NULL, 1);
$theinput->setAttribute("class","important");
$theform->addField($theinput);
$theinput = new inputBasicList("type",$therecord["paymenttype"],array("Cash"=>"cash","Credit"=>"credit"), "Payment Type");
$theinput->setAttribute("class","important");
$theform->addField($theinput);
The content of the type and quanitity fields get insert into the database perfectly, but absolutely nothing gets inserted from the smartsearch field.
Why? How would I start to troubleshoot this?
I think in this case you would need to manually add this value to the array that is persisted to the database. So:
$variables["product"] = the value you want
So if you san. To persist the name assign Ds-chooseproducts. If you want to persist the id then use chooseproducts from the array.

Categories