php - Echoing success after uploaded -
i want echo "success" on same page form on if uploaded correctly, im new php dont know how done?
index.php
<form action="upload" method="post"> <input name="field1" placeholder="first name" type="text" /> <input type="submit" name="submit" value="submit"> upload.php
$content = "".$_post["field1"]; $fp = fopen("upload/test.txt","wb"); fwrite($fp,$content); fclose($fp); i need redirect same page index.php
if need echo on same page , don't want use ajax there 2 possible solution.
a) use parameter on upload script, , thread on index.php (as @brad said @ comment)
upload.php
$content = "".$_post["field1"]; $fp = fopen("upload/test.txt","wb"); fwrite($fp,$content); fclose($fp); header('location: /index.php?m=success'); index.php
//check message parameter if (isset($_get['m']{0}) && $_get['m'] === 'success') echo 'form uploaded successfully'; //display upload form echo '<form action="upload" method="post">' . '<input name="field1" placeholder="first name" type="text" />' . '<input type="submit" name="submit" value="submit">'; b) within same script index.php
//check if form submitted if (isset($_post['field1'])) { $content = "".$_post["field1"]; $fp = fopen("upload/test.txt","wb"); fwrite($fp,$content); fclose($fp); //do else } //display form. use index.php action, instead of upload echo '<form action="/index.php" method="post">' . '<input name="field1" placeholder="first name" type="text" />' . '<input type="submit" name="submit" value="submit">'; update after comment:
when open file fopen function, define 2 arguments. first argument filename, , second argument flags.
you need 'a' (append) flag, instead of 'w' (write):
- 'a' open writing only; place file pointer @ end of file. if file not exist, attempt create it.
- 'w' open writing only; place file pointer @ beginning of file , truncate file 0 length. if file not exist, attempt create it.
take here
Comments
Post a Comment