1

I am using php to update some myqsl values from an html form and after the update i want to redirect the user to the page where he/she clicked 'update'. In order to accomplish that i am using the following line

echo "<script>var prevUrl='<?php echo $_SERVER['HTTP_REFERER'];?>';alert('Updated movie $mv !'); window.location.href=prevUrl;</script>";

but i get the following error Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in

I tried using \ as escape character for the ' in HTTP_REFERER but i had no luck. I tried writing the var with single and double quotes but still no luck.

Any suggestions?

mnmbs
  • 353
  • 3
  • 13
  • 1
    You have an echo inside an echo. Did you mean to do that? – Don't Panic Jan 14 '19 at 23:29
  • Yes. the first echo is because i am writing php and the second is because i want to print this variable an see its value. – mnmbs Jan 14 '19 at 23:30
  • 1
    You don't open PHP _in_ PHP. That doesn't make sense. Use concatenation, or variable interpolation, or printf and alike. – Jonnix Jan 14 '19 at 23:30
  • The inner `` tags and the `echo` do not do anything in that string. If you fix the error, they'll actually be in the page source, not executed. You're getting the error because the array key is quoted. You can use `echo " – Don't Panic Jan 14 '19 at 23:34
  • Use one of these two methods: https://ideone.com/MCAUfv –  Jan 14 '19 at 23:41
  • @ChrisG Thank you. That helps! – mnmbs Jan 14 '19 at 23:44

1 Answers1

2

You have a a php tag and echo command inside your echo command, use variables in your echo like this fo rexample

echo  "<script>var prevUrl='".$_SERVER['HTTP_REFERER']."';alert('Updated movie $mv  !'); window.location.href=prevUrl;</script>";
FroboZ
  • 437
  • 1
  • 6
  • 17
  • That solves my issue but i still cant redirect to the page i want. Maybe you can help me a little bit. I have an array of many results that are organised in pages. Each result has an update button. When you press the update button you go to another page where you can update the values. After updating i want to go back to the results in the same page i was before. The above redirects me back to the update page. – mnmbs Jan 14 '19 at 23:36
  • A simple qway to do that would be to use a get variable something like index.php?lastURL=someurlhere and then in your php code you access that url with $_GET['lastURL'] – FroboZ Jan 14 '19 at 23:44