Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best thing you can do is make the file upload using another form (below your main form) witch sends the uploaded file path to the main form -when it success-.</p> <p>Look the code in <a href="http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html#comment-form" rel="nofollow">here</a> and I make this changes to work like yours:</p> <p>In the head</p> <pre><code>&lt;script type="text/javascript" src="js/jquery-1.7.1.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="js/jquery.form.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { $('#photoimg').live('change', function() { $("#preview").html(''); $("#preview").html('&lt;br/&gt;Colocando archivo...&lt;br /&gt;&lt;img src="img/loader.gif" alt="Uploading...."/&gt;'); $('#imageform').bind('submit', function(e) { e.preventDefault(); // &lt;-- important $(this).ajaxSubmit({ target: '#preview', success: function() { $('#file').val($('#newimg').attr("src")); } }); }).submit(); }); }); }); &lt;/script&gt; </code></pre> <p>Image form:</p> <pre><code>&lt;form id="imageform" method="post" enctype="multipart/form-data" action='class/db_manager.php'&gt; &lt;label for="alias"&gt;*Imagen&lt;/label&gt; &lt;input type="file" name="photoimg" id="photoimg" /&gt;&lt;input type="hidden" name="MM_insert_img" value="imageform" /&gt; &lt;/form&gt;&lt;p&gt;Image&lt;/p&gt;&lt;div id='preview'&gt;&lt;/div&gt; </code></pre> <p>Main form:</p> <pre><code>&lt;form action="&lt;?php echo $editFormAction; ?&gt;" method="POST" enctype="multipart/form-data" name="f_insert_cliente" id="f_insert_cliente"&gt; &lt;label for="file"&gt;Imagen&lt;/label&gt; //when file uploads, this is where we will place the relative path &lt;input name="file" type="text" id="file" value="" /&gt; &lt;/form&gt; </code></pre> <p>PHP file</p> <pre><code>if ((isset($_POST["MM_insert_img"])) &amp;&amp; ($_POST["MM_insert_img"] == "imageform")) { $path = "../img/uploads/"; $valid_formats = array("jpg", "png", "gif", "bmp"); if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") { $name = $_FILES['photoimg']['name']; $size = $_FILES['photoimg']['size']; if(strlen($name)) { list($txt, $ext) = explode(".", $name); if(in_array($ext,$valid_formats)) { if($size&lt;(1024*1024)) { $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext; $tmp = $_FILES['photoimg']['tmp_name']; if(move_uploaded_file($tmp, $path.$actual_image_name)) { echo "&lt;br /&gt;Vista previa:&lt;br /&gt;&lt;img src='img/uploads/".$actual_image_name."' class='preview' id='newimg'&gt;"; } else echo "failed"; } else echo "Image file size max 1 MB"; } else echo "Invalid file format.."; } else echo "Please select image..!"; exit; } } ?&gt; </code></pre> <p>So how does it works: </p> <ol> <li>user uploads the file and the event is handdled by the jquery function (remember to add in the head the scripts/js) </li> <li>php return the uploaded image tag code (or error) </li> <li>jQuery handdle the event and look for the image SRC attribute and set it as value of the another form text input</li> </ol> <p>Hope it helps, be free to ask :)</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      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