ARCLite_Studio Posted April 27, 2013 Share Posted April 27, 2013 I'm using a form to capture various information - assigning the form input to PHP variables and then replacing placeholders in an external file with the appropriate PHP variables from the form using str_replace array - But, for some reason it replaces the first variable (specifically *bizname) in the external file correctly and then replaces the others with NULL / Blanks, as if the other form data isn't being stored in the variables - Any ideas why the other variables are not being replaces with data? form capture php file <form id="form1" method="post" action=""> <div>What is the advertised name of your business?<input name="busName" type="text" value="Enter Business Name" size="40" maxlength="40" style="margin-left:10px;" /></div> <hr /> <div>Enter the NUMERIC street number for your business:<input name="busNume" type="text" value="Enter Numeric Address" size="40" maxlength="40" style="margin-left:10px;" /></div> <div>Please select street direction:<select name="streetDir" size="1" style="margin-left:10px;"> <option>N</option> <option>E</option> <option>S</option> <option>W</option> <option> </option> </select></div> <div>Please enter the name of your street location:<input name="busStreet" type="text" value="Enter Street Name" size="40" maxlength="40" style="margin-left:10px;" /></div> <div>Please select the street type:<select name="streetType" size="1" style="margin-left:10px;"> <option>RD</option> <option>ST</option> <option>AVE</option> </select></div> <div>Enter your city:<input name="busCity" type="text" value="Enter City Name" size="40" maxlength="40" style="margin-left:10px;" /></div> <div>Enter your state:<select name="busState" size="1" style="margin-left:10px;"> <option>AL</option> <option>AK</option> <option>AZ</option> </select></div> <div>Enter your zip code: <input name="busZip" type="text" value="" size="10" maxlength="5" style="margin-left:10px;" /> </div> <hr /> <input type="submit" value="submit" > </form> PHP CODE in form capture file <?php $busName = $_POST["busName"]; $busNume = $POST["busNume"]; $stDir = $POST["streetDir"]; $busStreet = $POST["busStreet"]; $stType = $POST["streetType"]; $busCity = $POST["busCity"]; $busState = $POST["busState"]; $busZip = $POST["busZip"]; ?> <?php if (!empty($_POST)) { $myFile = "template.php"; $file_contents = file_get_contents($myFile); $fh = fopen($myFile, 'w') or die("can't open file"); $varibs = array('*bizname','*hn','*dir','*street','*stt','*busCity','*busState','*busZip'); $details = array($busName,$busNume,$stDir,$busStreet,$stType,$busCity,$busState,$busZip); $file_contents = str_replace($varibs,$details,$file_contents); fwrite($fh, $file_contents); fclose($fh); } ?> Last but not least the template PHP file being opened and written into (Just the relevant part - everything else stripped out) <div id="bname" class="bdetail bold fs19" itemprop="name">*bizname</div> <span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"> <div id="baddr" class="bdetail" itemprop='streetAddress'>*hn *dir *street *stt</div> <div id="bregion" class="bdetail" itemprop='addressLocality'>*busCity, <span itemprop='addressRegion'>*busState</span>*busZip <span itemprop='postalCode'></span></div> Quote Link to comment Share on other sites More sharing options...
Barand Posted April 27, 2013 Share Posted April 27, 2013 Strange, you're having a problem with str_replace() and what bit of code don't you show? Quote Link to comment Share on other sites More sharing options...
ARCLite_Studio Posted April 27, 2013 Author Share Posted April 27, 2013 Only code thats not showing is standard HTML header, meta tags, title etc. etc. -- and on the form capture part the actual form is like 30 fields, I have reduced to just 5 or so in this question in order to keep the code small and to the point. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 27, 2013 Share Posted April 27, 2013 You should use straight PHP or a templating system, rather than trying to read the contents of a file into a string, parse it, and then re-write the file. Quote Link to comment Share on other sites More sharing options...
ARCLite_Studio Posted April 27, 2013 Author Share Posted April 27, 2013 Jessica, I rarely use PHP or database languages, Typically I am straight HTML/CSS with occasional javascript for certain things - But on occasion I do encounter situations where PHP or similar languages are needed. Unfortunately I simply don't have the time to dedicate to learning straight PHP as I would like too, so until such time as that changes I am left with having to deal with one off situations like this one. I appreciate the effort to "reply with tips and hints on how to find the solution yourself." but I have spent the better part of 2 days building. testing, failing, reasearching and correcting to get to where I am now. So please understand that this is a request for a more involved answer from someone. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 27, 2013 Share Posted April 27, 2013 You're doing it wrong. The way you are approaching the problem is completely wrong. Use a template system or just use PHP. You're already using PHP and you have the variables. Just echo them. Quote Link to comment Share on other sites More sharing options...
Solution ARCLite_Studio Posted April 27, 2013 Author Solution Share Posted April 27, 2013 Found the problem - wasnt calling the $POST data correctly after the first variable. - Thanks for the effort. Jessica, You know think about it - your telling someone who is not a PHP programmer, that they are programming in PHP wrong. I kinda figured that part out already which is why I sought out help. I suppose if I was approaching the problem "correctly" or programmatically correct then I likely wouldn't be needed help, right? Anyways, it wouldn't hurt to give an example of the proper code instead of just stating the obvious. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 27, 2013 Share Posted April 27, 2013 Considering you're still using that approach, then clearly you didn't figure out that its wrong. You should not read the contents of a file then rewrite the file to do something that PHP is entirely designed to do - print a variable inside HTML. Did you bother to google for templating systems? Did you try anything besides the code you've posted, such as using echo? It's as simple as removing your file processing code, changing the HTML to include PHP echo statements, and including the file. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.