I'm really new to PHP and Im trying to append Nodes to XML using PHP.
This is my comments.php file:
<?php
$date = "http://jdrag.x10.mx/comments_file.xml";
$xml = simplexml_load_string($data);
$commentt = $xml->addChild("comment");
$name = $_POST["cname"];
$email = $_POST["cemail"];
$comment = $_POST["comment"];
$commentt->addChild("name", $name);
$commentt->addChild("email", $email);
$commentt->addChild("commentInside", $comment);
echo $xml->saveXML();
?>
My comments.html is:
<form action="comments.php" method="post">
Name: <input type="text" name="cname" />
Email: <input type="text" name="cemail" />
Comment: <input type="text" name="comment" />
<input type="submit" />
</form>
As you can see my XML file is simple the comments tag: http://jdrag.x10.mx/comments_file.xml
But when I submit the form I get this error:
Fatal error: Call to a member function addChild() on a non-object in /home/jdragx10/public_html/comments.php on line 5
And because i'm new to PHP I really don't know what it means or how to fix it.
Thanks in advance to anyone who can fix my code.
It looks like you want to be using simplexml_load_file().
The simplexml_load_string() function takes the XML document as a string whereas simplexml_load_file() takes the path to a file containing the XML, as you have.
You also have some problem with variable names, the path to the XML file is held in $date whereas you try to load the XML using the variable $data.
Finally, be sure to turn on (and to the maximum level) error reporting while you are writing your code. The code above gives a notice message ("Notice: Undefined variable: data…"), not just a fatal error message.
Set the values display_errors and error_reporting to On and -1 respectively, either in your php.ini file or using ini_set().
Related
I am having an error in my PHP code and I need help. If anyone knows the solution to this issue, please help me. The error message I am seeing is
[Fatal error: Uncaught BadMethodCallException: Method createtemplate is not defined. in C:\xampp\htdocs\php\vendor\phpoffice\phpword\src\PhpWord\PhpWord.php:148 Stack trace: #0 C:\xampp\htdocs\php\index1.php(10): PhpOffice\PhpWord\PhpWord->__call('createtemplate', Array) #1 {main} thrown in C:\xampp\htdocs\php\vendor\phpoffice\phpword\src\PhpWord\PhpWord.php on line 148].
This is my code
<!-- HTML form for selecting the Word document to convert -->
<form method="post" enctype="multipart/form-data">
<label for="word-file">Select Word document:</label>
<input type="file" name="word-file" id="word-file">
<input type="submit" value="Convert">
</form>
<?php
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the uploaded Word document
$wordFile = $_FILES['word-file']['tmp_name'];
// Include the PHPWord library
require_once 'vendor/autoload.php';
// Create a new PHPWord object
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
// Load the Word document
$document = $PHPWord->loadTemplate($wordFile);
// Save the document as a PDF
$document->saveAs('converted.pdf');
// Show a message to the user
echo '<p>The Word document has been converted to PDF.</p>';
}
?>
have tried many solutions but nothing works....
I think you can use save method not to loadTemplate...
please replace your code...
From:
$document = $PHPWord->loadTemplate($wordFile);
$document->saveAs('converted.pdf');
To:
$document = $PHPWord->save($wordFile);
Through the log error, I think you didn't clarify method createtemplate in your PhpOffice\PhpWord\PhpWord.php file!
So it should be worked if you declare that method!
I have a book where is a sample for creating a whole new picture, from a random picture given by the user, and I think there is something wrong in the example, at least I can't use it. The error code gave back by the PHP in the browser:
Fatal error: Uncaught Error: Call to undefined function imagecreatefromstring() in C:\xampp\htdocs\kepkonvertalas.php:13 Stack trace: #0 {main} thrown in C:\xampp\htdocs\kepkonvertalas.php on line 13
The HTML form:
<form action="kepkonvertalas.php" method="post" enctype="multipart/form-data">
<input type="file" name="kep"><br>
<input type="submit" value="kuldes">
</form>
The PHP part:
<?php
$fajl = $_FILES['kep']['tmp_name'];
$kep = file_get_contents($fajl);
$forraskep = imagecreatefromstring($kep);
$szelesseg = imageSX($forraskep);
$magassag = imageSY($forraskep);
$ujmagassag = 400;
$ujszelesseg = $ujmagassag*($szelesseg/$magassag);
$ujkep = imagecreatetruecolor($ujszelesseg, $ujmagassag);
$eredmeny = imagecopyresampled($ujkep, $forraskep, 0, 0, 0, 0, $ujszelesseg, $ujmagassag, $szelesseg, $magassag);
imagejpeg($ujkep, "ujkep.jpg");
?>
<img src="ujkep.jpg" alt="">`
What is suspect, is that this extension is not implemented into my php.ini, but I cant find any proof for it, either solve the problem. Thanks for the help!
I tried to implement a picture from an URL, and do the conversion on that, which means I deleted the
$fajl = $_FILES['kep']['tmp_name'];
$kep = file_get_contents($fajl);`
And I've made a variable with a picture URL, and in the
$forraskep = imagecreatefromstring($kep);
I just changed like this:
$forraskep = imagecreatefromstring(file_get_contents($src);
Well the next problem was, that the $forraskep variable was not defined.
I expect an output where I have the picture with the size of the given parameters.
The solution was actually kinda easy:
open php.ini
search for "extension=gd"
delete the semicolon
restart apache server
I have a simple HTML form using json_encode to send a PHP variable (via value=) to a file and then using json_decode to extract and echo the results. I am battling to get the correct syntax or method to decode the json_decode in the object environment, required by Opencart. It works fine when I use the procedural method below.
I have attempted various syntax changes, but they return errors, so I believe that the syntax is incorrect, or my method cannot be done this way.
1st Code is the Procedural method that returns the correct result.
2nd code is the OOP method which fails. - (assume syntax is wrong.
Code Working:-
<form id="myForm" action="radio_result.php" method="post"
enctype="multipart/form-data">
<input type="radio" name="service" value="<?php echo
htmlentities(json_encode($service_onx));?>"> ONX
//additional code excluded.
radio_result.php // not all code shown
<?php
if(!empty($_POST['service'])) {
$service = json_decode($_POST['service'], true);
print_r($service);
Code failing:-
<form id="myForm" action="index.php?route=checkout/checkout"
method="post" enctype="multipart/form-data">
<input type="radio" name="service" value="<?php echo
htmlentities(json_encode($service_onx));?>"> ONX
checkout.php // not all code shown
$this->session->data['service'] = (isset($this->request-
>post(json_decode(['service'])))) ? $this->request->post['service'] :
"not_set";
$data['onx'] = $this->session->data['service'][0];
$data['eta'] = $this->session->data['service'][1];
Error result:-
Fatal error: Cannot use isset() on the result of an expression (you can
use "null !== expression" instead) in
C:\wamp64\www\catalog\controller\checkout\checkout.php on line 101
I would like to get the json_decode working in the Opencart framework
checkout.php so that I can use the reult further.
If I understand you correctly, you need an object?If so you can first do
$service = json_decode($_POST['service'], true);
And than cast this array as an object:
$serviceObject = (object) $service;
And you will have an object. Try it out.
Had a similar problem. Solved it by also encoding/decoding on base64. base64_encode(json_encode($string)) and the json_decode(base64_decode($string))
I am trying to send back the last inserted id after I insert some data into my database. I am successfully entering the data into the database, but there is no response data being sent back. I am using Php PDO and it is hosted on Google App Engine and my headers are set to application/json in the dbconnect.php file. What do I need to add to make it successfully send back response data.
<?php
require('dbconnect.php');
$statement=$con->prepare('INSERT INTO customers (fname, lname) VALUES (:fname,:lname)');
$statement->execute(array(':fname' => $_POST['fname'], ':lname' => $_POST['lname'] ));
$orderId = $statement->lastInsertId();
$json = $json_encode($orderId);
echo $json;
$con = null;
?>
I got Notice: Undefined index: fname and Notice: Undefined index: lname and Fatal error: Call to undefined method PDOStatement::lastInsertId() – Moustache_Me_A_Question 10 mins ago
You have no name attributes for the form elements.
Therefore you need to add them to your elements.
Here is an example form you can base yourself on:
<form action="" method="post">
First name:
<input type="text" name="fname">
<br>
Last name:
<input type="text" name="lname">
<br>
<input type="submit" name="submit" value="Submit">
</form>
That is why you're getting undefined index notices.
$json = $json_encode($orderId); should read as $json = json_encode($orderId); - json_encode() shouldn't be a variable but the function itself.
http://php.net/manual/en/function.json-encode.php
Also:
$orderId = $statement->lastInsertId();
to
$orderId = $con->lastInsertId();
which is why you're getting this message:
Fatal error: Call to undefined method PDOStatement::lastInsertId()
lastInsertId() is a method of the PDO class, not the PDOStatement class.
which I found in Bill Karwin's answer when Googling the error:
https://stackoverflow.com/a/8580768/
I wrote a simple php script that basically echos the values put into a form on the page, later, I will have this write to a DB but I was working on troubleshooting it before I did that since I keep getting this warning.
Does anyone know why I am getting it? If I just fill in the fields and submit the form, the script works fine and the warning disappears.
PHP Function:
function quickEntry()
{
$subTitle = $_POST['subTitle'];
$subDetails = $_POST['details']; //This is line 13 in my code
echo "$subTitle";
echo "<br>$subDetails";
}
HTML / PHP Code:
<form method="post" action="">
<hr>
<h1>Quick Entry:<p></h1>
Subject Title:<br> <input type="text" name="subTitle"><br><br>
Subject Details: <p><textarea name="details" placeholder="Enter Details here..."></textarea><p><br>
<input type="submit" name="QuickEntrySubmit" value="Submit Quick Entry" /><br>
</form>
<?php
if (isset($_POST['QuickEntrySubmit']))
{
quickEntry();
}
?>
I know that I could disable warnings and I wouldn't see this, but I really just want to know why php is throwing the warning so I can fix the syntax appropriately and keep my code clean going forward. Full warning is:
Notice: Undefined index: details in C:\xampp\htdocs\test1.php on line 13
Thanks!
The reason why you are getting that error is because you are not checking whether the 'subTitle' and 'details' inputs have values in them.
Your code should work well like this:
function quickEntry(){
$subTitle = isset($_POST['subTitle'])? $_POST['subTitle']: null;
$subDetails = isset($_POST['details'])? $_POST['details']: null ; //This is line 13 in my code
if(!is_null($subTitle) && !is_null($subDetails)){
echo "$subTitle";
echo "<br>$subDetails";
} else{
//blah blah blah
}
You get the warnings because the $_POST variables with the indexes that you're checking for ($_POST['subTitle'] & $_POST['details']) aren't set, so the variables are empty as you reference something that isn't there.
You should do a check to ensure they are set first before trying to assign them to a variable:
$subTitle = (isset($_POST['subTitle']) && !empty($_POST['subTitle'])) ? $_POST['subTitle'] : null;
$subDetails = (isset($_POST['details']) && !empty($_POST['details'])) ? $_POST['details'] : null;
The above code will check to ensure the post index isset() and isn't empty() before assigning it to the variables.
NOTE
The variables are set when you submit the form because you are actually posting the data to the script.
You see the input attribute name? When you submit the form, they are the relevant $_POST indexes.
Just remember to ensure that the values aren't empty if you intend to print them to the markup.