Question: I have a hidden field where the question\_id is unique. I am trying to get that hidden field within jquery from the next page so that I can post a message of the form status. 

Then I will put that status in the HTML at the bottom of the form that was submitted.

form.php

```
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery File Upload</title>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script>

    function submitForm(upload_input_field){
        upload_input_field.form.submit();
        return true;
    }
    $(function () {
    });
  </script>
</head>
<body>
    <form action="uploadFile.php" target="uploadIframe" method="post" enctype="multipart/form-data">
        <input type='button' class='btn btn-default' onClick='submitForm(this)' value='Submit' />
        <input name='question_id' value='123' type='hidden' />
    </form>
    <iframe style="border:0;" id="uploadIframe" name="uploadIframe"></iframe>
    <div id="successMessage123"></div>
</body>
</html>

```

uploadFile.php

```
<?php   $question_id = $_POST['question_id']; ?>
  <script>
    $(document).ready(function(){
         $("[name=question_id]").val(var questionid);
        //alert(question_id);
        //$(('#successMessage_'+question_id), window.parent.document).html('<p>hidden value success</p>');
        $('#successMessage_'+question_id).html('<p>hidden value success</p>');
    });
</script>

```

Answer: 
You should rarely need to examine `$?` explicitly anyway, since doing that is the purpose of `if` (and other conditional builtins like `while`). The proper way to write that is

```
if ls /sdfg; then
    echo "Command succeeded: $?"
else
    echo "Command failed: $?"
fi

```

Notice how the failure case is now the `else` branch. (If you don't particularly care about preserving the failure exit code, you can use `if ! ls`... and keep the success branch in the `else`, but your question seems to be specifically about preserving it, which negation with `!` will not do.)
Collect the return value **immediately** after the command being tested if you're going to reuse it:

```
#!/bin/bash

# best-practice: collect *on the same line*, so any logging added in the future doesn't
# ...change your exit status.
ls /sdfg; retval=$?

if [ $retval -gt 0 ]; then
        echo "Command failed: $retval" # 
else
        echo "Command succeeded: $retval"
fi

```

---

However, the best practice is to directly operate on the command's exit status without needing to refer to `$?` at all:

```
if ls /sdfg; then
  echo "Command succeeded: $?"
else
  echo "Command failed: $?"
fi

```