Using php variable from one isset to another - php

I have a page where on form submit from previous page a div with something like invoice is created. I send that data to server and populate the invoice for print/generate PDF etc.
if (isset($_POST['kreiraj'])) {
$emailuser = $_POST["na_email"];
}
On same page I create with JS a PDF from canvas of that invoice and send it via Ajax for posting into PHP mail script witch is also on same page.
if (isset($_POST['fileDataURI'])) {
$pdfdoc = $_POST['fileDataURI'];
};
This all works but I have problems accessing PHP variables from each other.
I have read about function global and $_GLOBLAS[varables] but nothing seems to work for me. Can someone point out a way to do it?
TO be exact i need to access $emailuser from example code above in if (isset($_POST['fileDataURI'])) php mailing script.

Related

PHP Mail Sending a dynamic PHP Contents

I'm Creating a weekly Updates to my clients, and I want to include the latest (News, Articles, Photos) in this mail. So I created "webmail.php" page that's been created Dynamically using MySql, contains all my updates I want to send to my clients, with heavy css and html contents.
I'm using this PHP code in my script
ob_start();
include ('webmail.php');
$content = ob_get_clean();
$message = $content;
mail($email,$subject,$message,$headers);
The problem is I'm facing (500 Internal Server Error). I'm sure that my webmail.php contains no errors and this problem happens because this page has been created Dynamically.
Any Idea to solve this problem?. Thanks
I think you're missing a point there... If webmail.php is dynamically generated (which means it actually contains your information), then you can read its contents using :
$news = file_get_contents("webmail.php");
and just send $news as your email body. However, if webmail.php actually generates the content (which means it produces it when passed to the PHP interpretor), then maybe you should consider using a function in this file instead :
webmail.php
function latest_news(){
// Gets news from database, put them into $news.
return $news;
}
Then, on your first page (sending the email) :
include_once("webmail.php"); // Get the function.
mail("recipient#address.tld", "Our latest news", latest_news());

Get all content from a file, including PHP code

I'm making a small CMS for practice. I am using CKEDITOR and is trying to make it avaliable to write something like %contactform% in the text, and then my PHP function will replace it with a contactform.
I've accomplished to replace the text with a form. But now I need the PHP code for the form to send a mail. I'm using file_get_contents(); but it's stripping the php-code.
I've used include(); to get the php-code from another file then and that works for now. I would like to do it with one file tho.
So - can I get all content from a file INCLUDING the php-code?
*UPDATE *
I'll try to explain in another way.
I can create a page in my CMS where I can write a header and some content. In the content I am able to write %contactform%.
When I get the content from the database I am replacing %contactform% with the content from /inserts/contactform.php, using file_get_contents(); where I have the form in HTML and my php code:
if(isset($_POST['submit'])) {
echo 'Now my form is submitted!';
}
<form method="post">
<input type="text" name="email">
<input type="submit" name="submit">
</form>
Now I was expecting to retrieve the form AND the php code active. But If I press my submit button in the form it's not firing the php code.
I do not wan't to show the php code I want to be able to use it.
I still have to guess, but from your update, I think you ultimatly end up with a variable, which contains the content from the database with %contactform% replaced by file_get_contents('/inserts/contactform.php').
Something like:
$contentToOutput = str_replace(
'%contactform%',
file_get_contents('/inserts/contactform.php'),
$contentFromDatabase
);
If you echo out that variable, it will just send it's content as is. No php will get executed.
Though it's risky in many cases, if you know what you're doing you can use eval to parse the php code. With mixed code like this, you maybe want to do it like the following.
ob_start();
eval('; ?>' . $contentToOutput);
$parsedContent = ob_get_clean();
$parsedContent should now contain the results after executing the code. You can now send it to the user or handle it whatever way you want to.
Of course you'll have to make sure that whatever is in $contentToOutput is valid php code (or a valid mixture of php with php-tags and text).
Here is a link to the symfony Templating/PhpEngine class. Have a look at the evaluate method to see the above example in real code.
yes...
$content = file_get_contents( 'path to your file' );
for printing try
echo htmlspecialchars( $content );
From reading the revised question, I think the answer is "You can't get there from here." Let me try to explain what I think you will encounter.
First, consider the nature of HTTP and the client/server model. Clients make requests and servers make responses. Each request is atomic, complete and stateless, and each response is complete and usually instantaneous. And that is the end of it. The server disconnects and goes back to "sleep" until the client makes a new request.
Let's say I make a request for a web page. A PHP script runs and it prepares a response document (HTML, probably) and the server sends the document to my browser. If the document contains an HTML form, I can submit the form to the URL of the action= script. But when I submit the form, I am making a new request that goes back to the server.
As I understand your design, the plan is to put both the HTML form and the PHP action script into the textarea of the CKeditor at the location of the %contactform% string. This would be presented to the client who would submit the form back to your server, where it would run the PHP script. I just don't think that will work, and if you find a way to make it work, you're basically saying, "I will accept external input and run it in PHP." That would represent an unacceptable security exposure for me.
If you can step back from the technical details and just tell us in plain language what you're trying to achieve, we may be able to offer a suggestion about the design pattern.

How to use a javascript function in a pure php page

I am looking for a way to include a .js function with in a straight php page. By straight php I mean there is no html included.
Explanation of process if you will.
I have a page where employees must swipe their ID badge for access to a computer.
The employee swipes the badge, the magnetic strip is read, and the data sting is sent to the db to get the access levels etc. This works great unless I get a bad card read. What I have is a .js file that pulls the ID and the issue date of the badge from the data string and validates it before going to the db. If it fails it errors.
At the error point I will ask them to swipe the card again etc...
So back to the top, can I use this .js file in a php file.
If not, can someone point me to a library or chart where I can find the comparison values for js and php. (.js - var s = ""; | .php $s = ""; etc...)
Can't you implement the validation logic in php:
<?php
function validateId($id = null) {
// your validation code goes here
if($id != null) {
// Code to be executed for a successful swipe
echo "success";
} else {
// Code to be executed for a failed swipe
echo "An epic failure occurred. Please swipe again";
}
}
I think your best bet would be to install a server-side JS engine and then run it using system.
A reasonable example is Narwhal. If you install that on your server, then you can do something like this from PHP:
system('js /path/to/my/js/file.js',$retval);
The only thing you may have to modify is any case where your JS interacts with the browser (i.e. document.writes, DOM manipulation), but Narwhal has equivalents for interacting with the CLI.

Jquery and PHP rating function problem

I know that I was already posted similar question but I just can't find the answer and figure it out how to solve this problem.
I'm trying to customize Jquery Star Rating plugin (link text) but I do not know what to do to show the message based on response of PHP script.
Jquery script successfully send rating data to PHP scripts that query the database and based on that echo message of proper or improper rating.
What should I add to an existing JS code so I can get echo from PHP and base on that write a message on some DIV beside rating star?
Jquery:
$('#gal').rating('gl.php?gal_no=<?=$gal_no;?>&id=<?=$id;?>', {maxvalue:10,increment:.5, curvalue: <?=$cur;?>});
Simplified PHP code:
$br=mysql_query("SELECT count(gal) as total FROM ...")
if ... {
echo '0';
}
else echo '1';
}
Jquery code successfully transmitted data to PHP script and when the PHP done with checking data echo the result ('1' or '0'). How can I get this PHP result back to Jquery and based on them write a message? Something like:
if(data=="1")
{
$("#error").show("fast").html('not correct').css({'background-color':'#F5F5F5','border-color' : '#F69'});
}else{
$("#error").show("fast").html('correct').css({'background-color' : '#FFF','border-color' : '#3b5998'});
}
If someone has an idea...I would appreciate it.
You would have to modify the source of the rating plug-in, as it does not provide any way for you to handle return values of your script. Read the documentation of jquery.post method in jquery, and then try to understand rating's code. Notice that when calling post method rating plugin doesn't provide a callback method (in other words it just doesn't care what your php script returns). You could try to modify the code in such way, that it allows you to register your own callback method.

PHP work with JS Jquery

How do i make PHP work with JS?
I mean more like, i want to check if the user is logged in or not,
and if he is then it will:
$("#message").fadeIn("slow"); ..
How should i do this?
I have an idea maybe have a file that checks it in php, and then it echo out 1 or 0.
And then a script that checks if its getting 1 then do the message fade in.. But im not as so experienced to script that in JS
You cannot directly pass variables from Javascript to PHP because the PHP run on the server before it's sent to the client. But you can 'pass' variables from PHP to Javascript.
For example:
echo('<script type="text/javascript'> var phpvar = '.$variablefromphp.';</script>');
However, you can manipulate what javascript your browser will print. You can first check if the user is logged in in PHP, and based on that, conditionally print the HTML and Javascript.
For example
if($user->logged_in())
{
echo('<script type="text/javascript">$("#message").fadeIn("slow");</script>');
}
else
{
//php function
generateLoginBox();
}
I only javascript to enhance user experience. You should make your application work even when javascript turned off.
With the javascript enabled, you can add an enhanced experience, such as animated page element, AJAX request, etc.
In case of login state, you should have a way to know it in PHP script. Then in the output, you can have a conditional block that only executed if the login state is true. You can put anything you want here.
Javascript can be working in a static HTML page. You can use this to create a simple test for the code that you wrote, to see if it working as you want. Read the documentation in http://www.jquery.com/, there are many links there to many examples.

Categories