0
<?php

$name          = $_POST['name'];
$price         = $_POST['price'];
$absolute_path = "/Plants/List/index.html";

$PlantFile = fopen($_SERVER['DOCUMENT_ROOT'] . "$absolute_path", "a") or die("Unable to open file!");
$trimmedname = preg_replace('/\s+/', '', $name);
fwrite($PlantFile, "\n\n<div class='wrapper one $trimmedname' id='main'>\n<h1>$name</h1>\n<p>Price: $price</p>\n <?php if($_SESSION[id]) {?> \n<button>Delete $name</button>\n<script> var simple = \"<?php echo $trimmedname; ?>\"; $(\"button\").click(function(){$(simple).remove();}); </script> \n <?php } ?> \n</div>\n");

fclose($PlantFile);


?>

I keep getting the error:

Parse error: syntax error, unexpected '(', expecting variable (T_VARIABLE) or '$' in your code on line 9

Could anyone help? Thanks guys!

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    Are you trying to write php scripts into a file? Either way you need to review your concatenation and use of ` – Rasclatt Sep 30 '17 at 18:04
  • Yes its going into a new file. – A_Brave_Panda Sep 30 '17 at 18:09
  • So you are writing the ` – Rasclatt Sep 30 '17 at 18:10
  • Yes, basically im trying to add a part where if someone is logged in they can hit a delete button to remove some code. But if you aren't logged in you can't see that code. – A_Brave_Panda Sep 30 '17 at 18:11
  • Remove code right out of the file or from the browser page? I don't know your application, but it sounds like you might be going down the wrong road with removing code write from the file itself. – Rasclatt Sep 30 '17 at 18:14
  • Well im removing a div using the .remove() method on Javascript – A_Brave_Panda Sep 30 '17 at 18:15
  • Ok, then you don't want to rewrite the contents of a file directly. You want to just add that text block using a conditional, not with any `fwrite()` functions. You want to manipulate the view, not the core php file at the file level. – Rasclatt Sep 30 '17 at 18:17
  • Well i had it working ealier but when i added the delete method it stopped working. Like someone posts something, then i add it to the file. – A_Brave_Panda Sep 30 '17 at 18:19
  • Yeah, you really don't want to mess with the core `.php` file unless you really have a good reason. If you are trying to manipulate the user experience, you just have your `if` / `else` do that work to add and remove content, including javascript blocks. – Rasclatt Sep 30 '17 at 18:21
  • This script looks like you should be using a database but maybe aren't? – Rasclatt Sep 30 '17 at 18:23
  • No, i figured it out. I appreciate the help man! Thank you very much. – A_Brave_Panda Sep 30 '17 at 19:33

1 Answers1

1

I think the problem lies in your use of the curly braces in combination with the $ sign aka {$(simple).remove();}.

try replacing $ with \$ so that your jQuery $ sign wont be treated as a php variable.

Dmitry
  • 6,716
  • 14
  • 37
  • 39
Aki
  • 43
  • 1
  • 5
  • I tired that but it didn't work. Thanks for the idea though! – A_Brave_Panda Sep 30 '17 at 18:20
  • Oops. I lied lol it worked. Thanks very much! – A_Brave_Panda Sep 30 '17 at 18:21
  • Questions that are about basic PHP syntax errors should not be answered. They should be closed as a duplicate of [PHP Parse/Syntax Errors; and How to solve them?](//stackoverflow.com/questions/18050071) only. See: [Should one advise on off topic questions?](//meta.stackoverflow.com/q/276572/1768232). Off-topic questions can be closed and deleted, which could nullify your contribution anyway. – John Conde Sep 30 '17 at 21:16
  • I usually don't have problems with this but I looked at other questions of the such and I didn't find it 100% helpful. Plus either way it doesn't hurt to have someone else take a look at your code and give some tips. But thanks for the hint! Have a good one :D ! – A_Brave_Panda Sep 30 '17 at 22:19