I have a simple wordpress plugin that will take one or two inputs and display a chunk of HTML using a few pieces of meta data from custom posts in Wordpress (within the plugin admin menu).
I have it displaying the HTML output just fine on a new blank page, as well as displaying the input from $_POST when I tested it, although when I try and use get_post_meta() function using my $_post variables within that html output it draws a blank.
I'm using one PHP file, and the code is:
<?php
if (!isset($_POST['submit'])) {
?>
<h1>My Plugin</h1>
<form action="<?php echo plugin_dir_url( __FILE__ ).'myplugin.php'?>" method="post" >
Enter input:<input type="text" name="info" size="30">
<p><input type="submit" name="submit" value="Go">
</form>
<?php
}
else {
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<?php
$info = $_POST['info'];
?>
Information based on your input is:<?php echo get_post_meta($info, '_info', true); ?>
</body>
</html>
<?php } ?>
I'm a beginner at PHP and plugin development, so apologies for all the silly mistakes that are sprinkled in their most likely.
From my scouring on SO and other wordpress forums, I've got a hint that I might be taking the output out of the wordpress 'Sandbox' and therefore direct wordpress functions wouldn't work. So my question would be, what is the best way to generate my chunk of html with dynamic php on a separate blank page that still works with get_post_meta()?
I should mention that the code works correctly if I put it all on the first plugin page with no forms, and just force a few test variables to pull from the DB.
Related
I have just started to learn HTML and PHP, but have run into a road block while following beginner tutorials. I am attempting to have the user input numbers into a form on the HTML page, then press submit to redirect to a PHP page that displays the values. The PHP page shows up and successfully displays prepared text but displays nothing for the values.
HTML code:
<html>
<body>
<head>
<title>Practice Page</title>
</head>
<h1>Numbers</h1>
<p>Put numbers in the boxes</p>
<form action="welcome.php" method="post">
NumOne: <input type="text" name="oynumone"><br>
NumTwo: <input type="text" name="oynumtwo"><br>
<input type="submit" value="Submit" id="SubmitRegister" name="submit" />
</form>
</body>
<html>
PHP code:
<html>
<body>
Number one is <?php echo $_POST["oynumone"]; ?><br>
Number two is <?php echo $_POST["oynumtwo"]; ?>
</body>
</html>
Both of the files are simply in the same folder in my documents. I understand that I need a server to host PHP content; I have downloaded MAMP for this, but I don't yet understand how to use it.
Any help would be most appreciated.
Store both file name with .php extension AND/OR update Welcome.php like below -
Welcome.php
<?php
if isset($_POST['submit'])
{
$oynumone = $_POST['oynumone'];
$oynumtwo = $_POST['oynumtwo'];
echo "Number one is ".$oynumone;
echo "Number two is ".$oynumtwo;
}
?>
Also check this
I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.
If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.
My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?
NO you dont put your php scripts on the same page as your html file/s
Try this link for your reference =)
OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION
script1.php
<form action="script2.php" method="post" name="myform">
...
<input type="submit" name='submit_button' value="Submit" />
<input
</form>
---------------
script2.php
include 'script1.php';
if(isset($_POST['submit_button']
{.......}
Yeah You can put html and php in single document.
With the help of action.But it not the proper way.
In action you should mention this for writing html and php in same page.
<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>
You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.
If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.
e.g.
Your file: script.php
<?php
if(isset($_POST['btnsubmit'])) {
// Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
...
<input type="submit" name="btnsubmit" value="Submit" />
<input
</form>
What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly
header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...
Or another URI
header( 'Location: http://some.url/' );
If you really want to do this, here is a way:
<?php
if(isset($_POST)){
//do your php work here
}
?>
<html>
<form method='POST'>
//form elements here
<input type='submit'>
</form>
<!-- other html code -->
</html>
It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document
I know that the question has been asked before, why isn't my code saving, but this tutorial was recently recommended by a user here, and I have been following it from tuts plus, (obviously changing variables and options to fit my needs), but for some reason the little bar that says settings saved will come up, and the save button comes up, the form itself, it all looks right, but when I hit save, it is gone from the form. Keep in mind that I have only tried saving the settings for the first options, site logo url. Here is my code, it is properly included in the functions.php file.
PS: Sorry for it all being together, PHP and HTML, but that was the only way to get it all together in one post, plus, I like to put things in separate files after I finish.
<?php
if (!current_user_can('manage_options')) {
wp_die('You do not have permission to view this page, if you believe that this is a mistake, please contact your system administrator, or try closing the tab and come back.');
}
add_action("admin_menu", "setup_where_now_admin_menus");
function setup_where_now_admin_menus() {
add_menu_page('Front Door Theme Options', 'Front Door Theme Options', 'manage_options', 'where_now_elements', 'where_now_options') ;
}
?>
<?php
function where_now_options() { ?>
<div class="wrap">
<h2>Front Door Web Design Custom Theme Options</h2>
<form method="post" action="">
<h4>Site Logo URL</h4>
<p>Remember, this is the image used in places like your header, or anywhere else you want your logo to appear, and if you are having trouble remembering how to use this feature, you can always upload your new logo to the media section in Wordpress, get the URL from the specific images page, and then paste it here!</p>
<input type="text" name="site_logo" value="<?php echo $site_logo;?>" size="25">
<h4>Analytics Tracking Code</h4>
<p>Any code used for tracking purposes that is placed in the header goes here</p>
<input type="text" name="analytics_code">
<?php
$site_logo = get_option("where_now_site_logo");
if (isset($_POST["update_settings"])) {
$site_logo = esc_attr($_POST["site_logo"]);
update_option("where_now_site_logo", $site_logo);
?>
<div id="message" class="updated">Your Changes Have Been Saved</div>
<?php
}
?>
<input type="hidden" name="update_settings" value="Y" />
<input type="submit" value="Save settings" class="button-primary"/>
</form>
</div>
<?php
}
?>
Any ideas?
I recommend you to use this Plugin: Option Tree
It is very simple and doesn't need any coding.Just install it on your website that your theme is running,then you can manage your options easily.
I have 2 html wysiwyg editors on a wordpress admin page. Both use WP_EDITOR() function. The first one is hard coded into the page:
<form name="form1" id="form1" method="post" action="" style="display:block;">
<p>
<!-- editor here -->
<?php
wp_editor( 'CONTENT WILL APPEAR HERE!', 'addsometxt', array('textarea_name'=>'create_txt','textarea_rows'=>10,'wpautop'=>false));
?>
</p>
<p>
<input name="save" type="submit" class="button-primary" id="save" style="margin:5px;" value="Save Input" /></p>
</form>
The second one is generated dynamically with a PHP function using an AJAX call (wp_ajax_ and $.post). I've test the ajax call and know it works; so, for brevity, here's the php function:
<?php
function display_editor2() {
// grab data from database (data_from_db) and display in editor
wp_editor( $row->data_from_db, 'editsometxt', array('textarea_name'=>'edit_txt','textarea_rows'=>10,'wpautop'=>false));
}
?>
The problem is that even though the 2nd editor is displaying; it's missing all the tool bar buttons. See image below for illustration. Anyone know who to fix this?
I had the same problem.
When I add the code <?php wp_footer(); ?> in my footer.php, it works.
I had the exact same issue and solved it this way (WP 4.7):
First create an hidden editor in your template so WP load all the necessary files for TinyMCE (the ID doesn't matter):
<div style="display:none"><?php wp_editor('', 'hidden_editor'); ?></div>
Then after you appended the new editor to the DOM, use the following functions:
quicktags({id :'your_new_editor_id'});
tinymce.execCommand('mceAddEditor', true, 'your_new_editor_id');
Using tinymce.init didn't worked for me, as the new editor ID wasn't recognized. Those two lines reinstantiate the quicktags and add the new editor.
Probably you need to add media_buttons and tinymce parameter on your AJAX call.
Something like this:
<?php
function display_editor2() {
// grab data from database (data_from_db) and display in editor
wp_editor( $row->data_from_db, 'editsometxt', array('textarea_name'=>'edit_txt','media_buttons'=>true,'tinymce'=>true,'textarea_rows'=>10,'wpautop'=>false));
}
?>
I recommend you check wp_editor() Function Reference page at Wordpress Codex.
Hey I too had the same problem!
I just deactivated all the plug-ins which are installed by me and refreshed the page, and then I tried to edit the post/pages in the visual area also. Check once it will work for you. :)
I hade the same problem, using this:
<?php wp_editor(get_the_content()); ?>
By passing a ID (second parameter to wp_editor) I got the buttons.
Like this:
<?php wp_editor(get_the_content(), "with_a_ID_its_buttons_are_showing"); ?>
Recently installed wkhtmltopdf. Was trying to capture the entire page in its current state, however, the below method seems to navigate to the initial state of that page without all the input fields that the user has entered.
PHP
shell_exec('wkhtmltopdf http://localhost/www/bolt/invoice.php invoice.pdf');
I was wondering if someone knew of an implementation of wkhtmltopdf that captures the current state of the page including any text entered in the text fields??
I appreciate any suggestions.
Many thanks in advance!
wkhtmltopdf hits the page independently of your current browsing session. If you hit it like that, you're going to get what anyone would see when they first go to your page. Probably what you want to do is save the current page using an output buffer, and then run wkhtmltopdf on the saved page. Here's some sample code:
sub.php
<?php
$documentTemplate = file_get_contents ("template.html");
foreach ($_POST as $key => $postVar)
{
$documentTemplate =
preg_replace ("/name=\"$key\"/", "value=\"$postVar\"", $documentTemplate);
}
file_put_contents ("out.html", $documentTemplate);
shell_exec ("wkhtmltopdf out.html test.pdf");
?>
template.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h1>This is a page</h1>
<form action="sub.php" method="post" accept-charset="utf-8">
My Test Field:
<input type="text" name="test_field" value="">
<input type="submit" value = "submit">
</form>
</body>
</html>
Probably in the long run you should have some kind of base template that both pages would use, and one have some markers like value='%valueOfThisVariable%' in your input fields that you can replace with blanks when you present the fields to the user, and fill with the user data when you create the page that you want to write to pdf. Right now it's just going through and replacing all the name='this_name' with value='this_name->value'.