Reputation: 17
I am creating a simple page in php and fill the datas through xml rendering. I am new to php. My xml file is
data.xml
<painting>
<text1>Welcome</text1>
<text1>Test</text1>
<text1>Look here</text1>
<img_view1>img/myimg1.png</img_view1>
<img_view2>img/myimg2.png</img_view2>
</painting>
I have an html table which contains image and text. My table is
index.php
<table >
<tr>
<td>Name</td>
<td>Image</td>
<td>Type</td>
</tr>
<tr>
<td>Sports</td>
<td>img/sports.png</td>
<td><input type="submit" value="Btn" name = "submit" > </td>
</tr>
<tr>
<td>Tv</td>
<td>img/tv.png</td>
<td><input type="submit" value="Btn" name = "submit" ></td>
</tr>
</table>
And my requirement is when i press the row 1 button , the image present in the particular row needs to get update in img_view1 field of my xml file. All the other values needs to remain the same. When i press 2 nd button that row image needs to be updated in xml file.
I found updating xml file on button click from this and this works well. Here instead of manual entry in textbox i would like to update xml file from table on button click
I am very new to php and browsed whole day for this updation but cannot find the answer. Help me in achieving this.
Upvotes: 0
Views: 618
Reputation: 17
Finally i found the answer and it is very simple. on submit button add
<input type="submit" value="img_id" name = "image1" ></td>
and on php add something like
<?php
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('login_data.xml');
$element = $xml->getElementsByTagName('painting')->item(0);
$imgs = $element->getElementsByTagName('img_view1')->item(0);
?>
<?php
if ( isset( $_POST['image1'] ) )
{
$imgs->nodeValue = $_POST['bagrnd_img'];
htmlentities($xml->save('login_data.xml'));
}
?>
call this php page on form submit. This will change the image of our xml on button click.
Hope this may help someone :)
Upvotes: 0
Reputation: 2691
What you are asking has nothing to do with PHP. If you want to change an image (or in your case only a path in a table cell) on button click you have to do this on client side, so JavaScript will be the solution.
Assumed, you are using jQuery, the following could be rewritten to solve your problem:
var img = "";
function parseXml(xml)
{
img = $(xml).find("painting").find("img_view1").;
}
/* wait until site has loaded */
$(document).ready( function () {
$("#id-of-your-button").click( function () {
$.ajax({
type: "POST",
data: {'img_src1': $("#id-of-cell").text()}
url: "index.php",
dataType: "xml",
success: parseXml
});
}
});
Now you can get the value of $_POST['img_src1'] and insert it into your xml file (like the post of your link).
See jquery.com for more information
Upvotes: 0