Executing PHP Code in Contact Form 7 Textarea - php

I've got a contact form 7 form that I'm looking to execute php in the textarea field.
When I tested this with a normal form (ie not a plugin) it worked fine;
<textarea name="customer-issue" rows="10" cols="40"><?php if(isset($_GET['content'])) { echo $_GET['content']; } ?></textarea>
Does anyone know how you would go about being able to do this in CF7

In addition to the code of JpDevs:
He forgot some ' ' at setting the $html variable. This is working:
function cs7() {
$var=$_GET['content'];
$html='<p>'.$var.'</p>';
return $html;
}
add_shortcode('cs7', 'cs7');
Then just add [cs7] to your form.
When you use ' ', you have to write the variables outside by connecting with points:
$result = '<p>'.$var.'</p>';
When you use " ", you can write them inside:
$result = "<p>$var</p>";

Kindly have a look at below mentioned link :
https://wordpress.org/support/topic/contact-form-7-input-fields-values-as-php-get-viarables
Hope this helps for you

Make the code
$var=$_GET['content'];
to short code,
and the paste the generated shortcode in your contact form 7 text area
Eg:
functions.php
function cs7()
{
$var=$_GET['content'];
$html='<p>.$var.</p>';
return $html;
}
add_shortcode('cs7', 'cs7');
add [cs7] in contact form area

Related

My code in cf7 hook function is not executed. I put the code in wp-content/plugins/contact-form-7/includes/functions.php

I am using Contact Form 7 and trying to hook a function 'wpcf7_after_save' with my own custom function 'my_wpcf7_after_save' . To try , I am just putting some html tags to display on page.
But it looks like none if my code executes.
Initially , I tried to put just an error_log in my 'my_wpcf7_after_save' function and it worked fine as it gave error log page.
function my_wpcf7_after_save (&$WPCF7_Con<tactForm)
{
Echo "<html>";
Echo "<title>HTML With PHP</title>";
Echo "<b>My Example</b>";
//your php code here
Print "<i>Print works too!</i>";
}
add_action("wpcf7_after_save", "my_wpcf7_after_save");
I expected the HTML tags to be displayed on screen.

Cannot get posted variables to echo in the form

It seems my code is correct, however the posted variables in the form will not echo in the update user settings page in the form. I have echoed the posted ids from the input in the database but I cannot get the variables to show.
I have done this in Codeigniter fine but am trying to do it in pure php with my own framework.
$users = new Users($db); comes from my init.php file that is called at the beginning of the file below.
when I
<?php var_dump($user['first_name'])?>
I get Null
<input type="text" name="first_name" value="<?php if (isset($_POST['first_name']) )
{echo htmlentities(strip_tags($_POST['first_name']));} else { echo
$user['first_name']; }?>">
Hoi Stephen,
Try print_r($_POST["first_name"]); instead of var_dump();
or just for all:
print_r($_POST);
best regards ....
add this at the top of your html page
#extract($_REQUEST);
and put is just to check and after checking remove the below line
print_r($_REQUEST);
hope this help .

setting textarea to empty yet it reloads variable data on submission

If I set my text area in my edit page to blank and I hit submit, it reloads the page with an error as its supposed to but it also loads the summary text from my php variable. instead it should stay blank.
Here's what I've tried:
<textarea name="summarytxt" cols="52" rows="7">
<?php
if ( isset($_POST['updatebtn']) AND (!empty($POST['summarytxt'])) ){
echo #$POST['summarytxt'];
}
elseif ( isset($_POST['updatebtn']) AND (isset($POST['summarytxt'])) ){
echo #$POST['summarytxt'];
}
else{
$summary=stripslashes($data['summary']);
echo $summary;
}
?>
</textarea>
I was hoping the elseif condition will fix it but it doesn't seem to work:
elseif ( isset($_POST['updatebtn']) AND (isset($POST['summarytxt'])) )
The above to me meant if the button is clicked and the text area is set echo whatever is in the text area.
Any suggestions on what I'm doing wrong or how I can fix it?
Edit:
I basically want to know the following:
if say i have field summarytxt and an array called data['summary'] i need:
1. on page load, load from data['summary']
2. if the user clears the summarytxt, on page submit, it should be blank inside summarytxt
i have tried the following which seems to work...
<?php
if (!isset($_POST['updatebtn'])){
$summary=stripslashes($data['summary']);
echo $summary;
}else{
echo $_POST['summarytxt'];
}
?>
Now i'm having some trouble with my drop down box:
1. when i change it from a product to 'select' which is what you get in the add form, and i update the page. instead of sticking to select its loading the product it had when i opened the form.
Can somebody please show me a simple way of this? thanks!
So I would do this similar to (remember about php and html separation):
<?php
$summary = "";
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$summary = stripslashes($_POST['summarytxt']);
}
?>
<textarea name="summarytxt" cols="52" rows="7"><?php echo $summary; ?></textarea>

symfony image inside a button?

I'm trying to include an image inside a button using symfony1.4 with this code:
<?php
echo button_to(image_tag('icon.png')."button_name",'url-goes-here');
?>
But the result i get, instead of what i want is a button with "img src=path/to/the/icon.png button_name" as the value of the button. I've google'd it long enought and found nothing, so i'll try asking here.
In other words:
i'd like to find the way to generate html similar to:<button><img src=..>Text</button> but with a symfony url associated in the onclick option
How can i do it to put an image inside a button with symfony? Am i using the helpers wrong?
Thank you for your time!
You are using Symfonys button_to function incorrectly. From the documentation:
string button_to($name, $internal_uri, $options) Creates an
button tag of the given name pointing to a routed URL
As far as I can tell, the button_to function does not allow for image buttons. Instead, you will probably create the button tag yourself and use symfonys routing to output the url.
I finally created my own helper to display this kind of buttons. I know is not very efficient and flexible but works in my case. Here is the code
function image_button_to($img,$name,$uri,$options){
$sfURL = url_for($uri);
$sfIMG = image_tag($img);
if(isset($options['confirm'])){
$confirm_text = $options['confirm'];
$jsFunction = 'if(confirm(\''.$confirm_text.'\')){ return window.location=\''.$sfURL.'\';}else{return false;}';
}else{
$jsFunction = 'window.location="'.$sfURL.'";';
}
$onclick = 'onclick="'.$jsFunction.'"';
if(isset($options['title'])){
$title = 'title=\''.$options['title'].'\' ';
}else{
$title = '';
}
if(isset($options['style'])){
$style = 'style=\''.$options['style'].'\' ';
}else{
$style = '';
}
return '<button type="button" '.$onclick.$title.$style.' >'.$sfIMG." ".$name.'</button>';
}
With this function as helper, in the templates i just have to:
<?php echo image_button_to('image.png',"button_name",'module/actionUri');?>
hope this be useful for someone ;)

use php to change a html elements inner text

I have a basic form, which i need to put some validation into, I have a span area and I want on pressing of the submit button, for a predefined message to show in that box if a field is empty.
Something like
if ($mytextfield = null) {
//My custom error text to appear in the spcificed #logggingerror field
}
I know i can do this with jquery (document.getElementbyId('#errorlogging').innerHTML = "Text Here"), but how can I do this with PHP?
Bit of a new thing for me with php, any help greatly appreciated :)
Thanks
You could do it it a couple of ways. You can create a $error variable. Make it so that the $error is always created (even if everything checks out OK) but it needs to be empty if there is no error, or else the value must be the error.
Do it like this:
<?php
if(isset($_POST['submit'])){
if(empty($_POST['somevar'])){
$error = "Somevar was empty!";
}
}
?>
<h2>FORM</h2>
<form method="post">
<input type="text" name="somevar" />
<?php
if(isset($error) && !empty($error)){
?>
<span class="error"><?= $error; ?></span>
<?php
}
?>
</form>
If you want change it dynamically in client-side, there is no way but ajax. PHP works at server-side and you have to use post/get requests.
Form fields sent to php in a $_REQUEST, $_GET or $_POST variables...
For validate the field param you may write like this:
if(strlen($_REQUEST['username']) < 6){
echo 'false';
}
else{
echo 'true';
}
You can't do anything client-side with PHP. You need Javascript for that. If you really need PHP (for instance to do a check to the database or something), you can use Javascript to do an Ajax call, and put the return value inside a div on the page.

Categories