0

I've looked around trying to find a solution to clear a textarea from an outside php file.

Basically this is my form:

<iframe name="post_target" style="display:none;"></iframe>
       <form action="post_status.php" method="post" target="post_target">
            <div class="status_update">
                <div id="update_type"></div>
                <?php load_picture($_SESSION['profile_picture']); ?>
                <textarea class="scrollabletextbox" placeholder="Share your thoughts" onkeyup="textAreaAdjust(this)" name="status_update" id="status_update"></textarea>
            </div>
            <div id="update_options">
                <button id="post" name="post" onclick="javascript:postUpdate()">Post</button>
            </div>
        </form>

And it calls post_status.php who's structure is basically:

<?php   
function post_status($conn, $status){
    // function
}

$status = $_POST['status_update'];
post_status($conn, $status);
?>

<script type="text/javascript">
    // clear textarea
    document.getElementById('status_update').value = "";
    alert("passed clear");
</script>

What happens is the following : the php function executes properly, but the JS part doesn't. If I leave out ' .value = "" ' it works fine, if I don't then the alert never shows.

Does anyone have any idea concerning this issue ?

TanguyH
  • 171
  • 1
  • 12
  • 1
    Try: `document.getElementById("status_update").reset();` – devpro Sep 30 '16 at 14:27
  • The result is the same, the alert never executes – TanguyH Sep 30 '16 at 14:29
  • alert not coming after submission? – devpro Sep 30 '16 at 14:30
  • What happens is the textarea doesn't get cleared (and after that the alert never executes); if that's what you ask ? – TanguyH Sep 30 '16 at 14:31
  • ` – CD001 Sep 30 '16 at 14:35
  • Is the document ready for you to get the element `status_update`? Maybe it doesn't exist at this stage... Do you have an error in the console telling you it `cannot set property value of null`? – Henders Sep 30 '16 at 14:37
  • If *post_status.php* is a different file to the one that the form is on, it knows nothing about it. The JavaScript won't find the *status_update* element because it doesn't exist so attempting to call `.value` will result in a JavaScript error and termination and `alert()` won't get called. – CD001 Sep 30 '16 at 15:06
  • @CD001, post_status.php is indeed a different file. Isn't there any possible way to access the textarea from this file ? – TanguyH Sep 30 '16 at 16:00
  • Not in any meaningful way... and there's no reason to; the web is basically stateless, once you've left a page it no longer has anything to do with the state of your application **now**. If the problem is something to do with text remaining in it when the user hits the back button that's a different issue entirely. – CD001 Sep 30 '16 at 16:05
  • @CD001, in that case; is there any possible way to execute a js function doing that after return form `post_status.php`? What I mean is : the `postUpdate()` function is called, then `post_status.php`does its work. As the page is not refreshed (point of assigning an iframe target), how do I ensure the value clearing happens after execution of `post_status.php`? – TanguyH Sep 30 '16 at 16:09
  • You can target the parent page, with JS, using `window.parent` : http://stackoverflow.com/questions/2161388/calling-a-parent-window-function-from-an-iframe - so you could try something like `window.parent.getElementById("status_update").innerHTML = "";` – CD001 Oct 03 '16 at 08:02
  • @CD01, that doesn't solve the problem. The thing is : the form executes a JS function when it's submitted (That goes fine) and targets an iframe so that the page wouldn't reload when calling the php function in the action file (no problem here). What I would like to do is call a JS function after the execution of the php function in the action file in order to clear the textarea. (If it's done in the first JS function, the php function can't retrieve its content) – TanguyH Oct 03 '16 at 13:08

1 Answers1

0

You use status_update on class attribute (div) and id attribute (textarea)

you need to make a choice.

Julien Baldy
  • 397
  • 2
  • 4
  • 14
  • Nope ! That's the point of getting by id or getting by class .. I tried to change the id to see whether it would have any impact, it doesn't.. – TanguyH Sep 30 '16 at 14:32
  • That's not actually a problem, one's a `class` the other is an `id` (and `name`) - two completely different things. – CD001 Sep 30 '16 at 14:33
  • your html is on the same page than post_status.php ? – Julien Baldy Sep 30 '16 at 14:35
  • I basically meant what @CD001 said ;) @Julien Bady, my html is on a separate page ! The button in the form is what triggers the execution of `post_status.php` – TanguyH Sep 30 '16 at 15:57