Note that there are some explanatory texts on larger screens.

plurals
  1. POPhp to return value
    primarykey
    data
    text
    <p><strong>EDIT</strong>: Check at the end of this for the solution</p> <p>I am new to php, ajax and all other things :)</p> <p>My question is: is there a way for a php file to return a value?</p> <p>I have: </p> <ol> <li>a file "loadImages.php" containing the script to get the paths to images from the database.</li> <li>an image gallery where I want to load images via ajax.</li> <li>a database containing the path to the images (/images/image1.jpg, /images/image2.jpg).</li> <li>4 categories of images.</li> </ol> <p>What i'm trying to do is :</p> <p>When clicking on a link (example, first category), I want to call via jquery's ajax(), loadImages.php with the category passed via POST (cat0, cat1, cat2, ...)</p> <p>I want to return the value from this php file for example : <code>&lt;img src="image/image1.jpg" /&gt;</code>, so via javascript, I can retrieve this string. Using only <code>return</code> in my php file returns <code>XMLHttpRequest</code> when i'm putting the <code>ajax()</code> function in a variable instead of the string <code>&lt;img&gt;</code>.</p> <p>Is there a way to do it? Or maybe a better way, as I don't fully understand ajax.</p> <p>Thank you! Sorry for my bad grammar.</p> <p>Below is a more precise map of what i'm trying to do.</p> <p>JavaScript:</p> <pre><code>var test = $.ajax({ type: "POST", url: "loadImages12.php", data: "category=0", complete: function() { alert("COMPLETE"); }, error: function (){ alert("NOT LOADED"); } }); </code></pre> <p>PHP (loadImages.php)</p> <pre><code>function createThumb() { if(isset($_POST['category'])) { $imageQuery = mysql_query("SELECT * FROM t_pictures WHERE p_category = 0"); $thumbHtml = ''; while ($tempImageQueryFetch = mysql_fetch_assoc($imageQuery)){ $thumbHtml .= '&lt;a href="#" class="thumbnail"&gt;&lt;img src="ressources/images/' . $tempImageQueryFetch["p_fileName"] . 'Small.jpg" /&gt;&lt;/a&gt;'; } return $thumbHtml; } else { $noCategory = "NO CATEGORY TEST"; return $noCategory ; } } createThumb(); </code></pre> <p><strong>SOLVED</strong></p> <ul> <li><p>Php File </p> <p>function createThumb(){<br> //Mysql request<br> $someVar = //Result of request<br> return $someVar<br> } </p> <p>echo createThumb() </p></li> <li><p>Javascript </p> <p>$("#someDiv").load("loadImages.php", {category:0});</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload