1

The javascript cannot redirect when the subId value is a letter but if it is a number it works fine.

here's my code

if($sql_update==true){


         echo "<script type='text/javascript'>

                function alertAndRedirect(subId) {
                    alert('Updated');
                    window.location = '?tag=subject_entry&opr=upd&rs_id='+subId;
                }


                alertAndRedirect(".$_POST['sub_code_txt'].");
                </script>";

    }   

the variable subId only accepts integer or number from the post method alertAndRedirect(".$_POST['sub_code_txt'].");. How can I let javascript accept the string id so that I can redirect?

1
  • where is you javascript code where you are calling method? Commented Sep 23, 2015 at 4:02

3 Answers 3

1

Try to enclose in single quotes :

alertAndRedirect('".$_POST['sub_code_txt']."');
Sign up to request clarification or add additional context in comments.

Comments

1

You need to enclose your value in quotes '...':

alertAndRedirect('".$_POST['sub_code_txt']."');

Otherwise it will show up as:

alertAndRedirect(valueOfItem)

You want it to be enclosed in quotes for text values:

alertAndRedirect('valueOfItem')

Comments

0

Both the given answers are correct. Just added an enhancement in my answer to avoid a possible error. The posted variable may also contains single or double quotes so adding slashes would be a better handling.

alertAndRedirect('".addcslashes($_POST['sub_code_txt'])."');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.