I have a button when user click on that button it appends a dropdown list to the HTML and I have used PHP to retrieve data from database in fill the dropdown list, while i try this code it is not working for me! any idea?
$(".pageSheet").on("click", ".bt2", function () {
$(this).closest(".pageIn").append("<p>Image: <select name='image'>
<?php include'scripts/addImage.php'; addImg(); ?></select><br><button class='deleteCon'>Delete</button></p>"); i++;});
You cannot append <?php // code here ?> to an already rendered webpage using JavaScript and expect PHP to parse it. PHP parses the page while it is being handled by the webserver attending to the request. If you want JavaScript (client-side) to interact with something PHP generates from a Database (server-side), you need to look into AJAX.
Related
I've been looking around for solutions for this for quite sometime and sadly, haven't found a proper solution for it.
Here's the situation.
I have a Bootstrap modal that displays text ( let's call it 'header' ) depending on which button was clicked. The header value is picked up from the data-attribute of the button and using jQuery, I update the text for the respective modal. I hope I've been clear enough about this.
Now, I want to use the same header text in a PHP Script that queries my MySQL table with this 'header' value in the LIKE clause of my query as '%header%'.
Seems simple enough but I can't get my head around it.
Here are some issues that I face:
The header value is empty in the PHP script when I try to pass it to the script. I tried using script_tags to strip the tags around the HTML of the header and just extract the text and also tried PHP DOM but it did not work.
Here are the basic steps for further clarity:
I have a PHP script that needs to get a text value from the HTML on the same page.
This text value was picked up from a data-attribute of a button and updated via jQuery to the HTML.
This text value must be included in the LIKE clause of MySQL query in the PHP script for any updates to the table.
I can handle everything else except the extraction of text and sending it to the PHP script part.
Thank you.
Adding Code for reference:
This is the HTML code that houses the data-attribute:
The $project_title variable was set previously and appended to the button.
echo "<a class='delete_icon' data-project_title='$project_title'><i class='fa fa-trash'></i></a>";
This is the jQuery code:
$('#project_profile_modal').on('show.bs.modal', function (event) {
var del_icon = $(event.relatedTarget);
var project_title = del_icon.data('project_title');
modal.find('h4').text(project_title);
}
This is the excerpt of the PHP script that is relevant:
<?php
$result = "SELECT content FROM my_table WHERE content LIKE '%$project_title%';
?>
Question is: how do I get the text contained in h4 to $project_title/my LIKE clause.
Assuming button looks something like:
<button class="modal-button" data-text="..." data-row_id="...">
Then in click handler you gather all the data and send to server:
$('.modal-button').click(function(){
var data = $(this).data();
// send to server
$.post(url, data, function(response){
// update modal header and content
$('modal-header-selector').text(data.text);
$('modal-content-selector').html(response);
})
})
It is possible to create a pdf file after a PHP form has been submited?
I need to print an order details after a php form is submited.
At this time i have a small jquery script that allows to print page that it prints only html content the php variables not.
$(document).ready(function() {
$(".btnPrint").printPage();
});
Any advice much appreciated.
I have a single page of HTML that includes PHP code.
I am using the PHP to send data from a form to the same page using $_SERVER['PHP_SELF'] as the form action.
There is an isset($_POST... condition in the PHP that detects if the page has loaded with data via POST. If this is true, the PHP compares the value sent to a set maximum value.
If the data is less than the maximum value, a table is displayed.
If the data exceeds the maximum value, I would like to display an error message using jQuery: $('.error_box').append("Error");
The error message is not displaying. I think this is because the PHP is trying to make changes to .error_box before it has loaded.
How can I make sure that the error display function is available, given that I am validating the form data via PHP as soon as the page loads?
You don't have to use javascript or jQuery to add content to '.error_box'
You should add it with PHP directly.
EDIT:
You must know that you can't directly execute javascript via PHP.
Here is how your page is created then rendered (this is simpler than reality) :
Your server receive the request to display a page
The PHP code is executed and output an HTML (+CSS +JS) page
The output is send back to the client who ask for page
The client browser parse the HTML and render it using css rules you specified
The javascript is executed
Wrap it in a document ready function, like so:
$(document).ready(function() {
$('.error_box').append("Error");
});
Edit:
AMDG is probably right also...
I know I could use a php include to read the html from a file on the server but how do I write a file to the server once the user clicks to navigate to the next page?
I have a div that is changed by jquery on the 1rst page. I want to read the changed div when the user clicks to go to the 2nd page and write the html from the 1rst page to the 2nd page.
You can use combination of javascript and php code.
jquery
get the value of changed div and place it into a hidden field, wrapped within a form tag and submit the form to the next page
php
and on next page, get your hidden field value from $_post array and display it.
So you have a div that is changed via jQuery:
<div id="something">something here</div>
To access the HTML inside your div,
var myHTML = $('#something').html();
Then Use AJAX to send the value to the second page:
$.ajax({
url: 'secondpage.php',
data: {
'key' : myHTML
}
type: 'post'
});
In secondpage.php, check for $_POST['key'] as follows
if( isset($_POST['key']) ) {
// myHTML was sent successfully
}
Not sure if it's a good idea to do this as users are clicking because it would take a while for the document to get updated, and what if multiple users were trying to access the same page? You can use something called a cronjob, which basically executes PHP from your site's server at a specified time interval. I did this to update my website with my Twitter feed every 10 minutes, but doing it on every click would be too slow. What exactly are you trying to do?
When the user clicks on "go to next page" link, send AJAX request to
some PHP file.
The AJAX request should contain the div html (use
jQuery: $(this).html())
In the php file write the html, and return
information to AJAX (true/false).
When AJAX success go to the next page.
I want to display the photos according to the album selected. But, I don't want to post the page, I want to just change the div.
This is my script:
<script type="text/javascript">
function replaceContent(divName, contentS) {
document.getElementById(divName).innerHTML = <?php echo get_pictures_from_album($fb, $albums, contentS); ?>;
}
</script>
And this is the select tag that invokes it:
<select name="album" size= "1" style="width:210;" onchange="replaceContent('photos', this.options[this.selectedIndex].value);">
<?php get_albums_select_list($albums); ?>
</select>
<div id = "photos">
<?php echo get_profile_pictures($fb, $albums); ?>
</div>
I understand from a reading that I have done that the problem might be connected to javascript Vs php variable types.
Please advise.
Looks like you are looking for an AJax call to an PHP script that retrives the data for the appropriate album selected and THEN update the div with the callback function.
Ajax + PHP basics
You are mixing Clientside and Serverside Code here. The function replaceContent is called after the page (and the php code) was loaded. You would need an Ajax Call for that if you need more information about that:
Ajax Tutorials on Google
What you are doing is not possible because PHP code runs before (on the server because PHP is server-side language) javascript code.
You will have to resort ot AJAX for that.