boumbo Posted April 25, 2012 Share Posted April 25, 2012 I have a search box with an input box. When the user enters a string in this box and then when it clicks submit, the form needs to go to the http://ww.mywebsite.com/wp-content/uploads/<?php echo $_POST["name"]; ?>.jpg where $_POST["name"] is what the user entered. how to make it work?? <form action="http://ww.mywebsite.com/wp-content/uploads/<?php echo $_POST["name"]; ?>.jpg" method="post" target="_blank"> <!-- form fields here --> <INPUT TYPE = "Text" NAME = "name"> <input type="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/261560-beginner-question-form/ Share on other sites More sharing options...
trq Posted April 25, 2012 Share Posted April 25, 2012 That's not going to work because $_POST['name'] wont exist until the form has been submitted. You will need to post the form to an intermediate page and redirect from there. Quote Link to comment https://forums.phpfreaks.com/topic/261560-beginner-question-form/#findComment-1340308 Share on other sites More sharing options...
jcanker Posted April 25, 2012 Share Posted April 25, 2012 You can't do this directly in PHP, but you can do this in AJAX, which would get the value of the input element named "name" (with an id "name" to be more specific...) Now that I think about this, tho, you CAN do this in PHP using header on the php page that is processing the form after user clicks submit. Make the form action go to the page to process the form, then redirect from there header('Location: http://www.example.com/'); Just alter that so that your URL is properly formed instead of the example.com. One word of warning can't can't be said strongly enough--header() will NOT WORK if you output anything before you call it. You can process all you want, create variables, assign values, etc, but you cannot output anything before you call header() Quote Link to comment https://forums.phpfreaks.com/topic/261560-beginner-question-form/#findComment-1340310 Share on other sites More sharing options...
boumbo Posted April 25, 2012 Author Share Posted April 25, 2012 I am beginner at this .. can you please elaborate.. how to use header??? it is in the form?? Quote Link to comment https://forums.phpfreaks.com/topic/261560-beginner-question-form/#findComment-1340311 Share on other sites More sharing options...
jcanker Posted April 25, 2012 Share Posted April 25, 2012 No, your form has to be processed by php. Which php page processes this form is determined by the ACTION attribute of your web form. User clicks submit, and the form data is submitted to the php page designated in the ACTION attribute. That php page gets the $_POST or $_GET data (depending on which method you set in the form) as an array. From that point it's easy to access it. In your code you named the input block "name" so it will now be in the $_POST under the key name. We access it by: $_POST['name'] $myvariable = $_POST['name'] So, if all you have to do is get the value entered by the user in that form and use that to complete the URL you will send them to, it's as simple as <?php $url = "http://ww.mywebsite.com/wp-content/uploads/".$_POST['name'].".jpg"; header($url); That will take them to the page to process the form when they click submit and then redirect them to the specified jpg file, which will cause the save/download prompt. Consider here that at that point you've stranded your user 2 pages away from any legitimate content of you page since you sent them directly to a file. If they click the back button, the first back button click will revert to the redirect page, sending them back to the jpg, etc. You should always redirect to another page, not a file. Of course, I have to issue the standard warning that you should NEVER trust user input (do a google search for Bobby Tables) so you should at least sanitize the $_POST data before you access what they put in the form. I'm not sure what this project is, but it sounds awfully quirky to have them be directed to a .jpg file based on what they type in a form. How will they know the filename exists? It may be inconsequental to your question, per se, and I may be wrong (wouldn't be the first time) but I have to caution that this question stinks to high heaven of trying to do something that php wasn't intended for and/or using the wrong workflow to achieve the end result you want. Quote Link to comment https://forums.phpfreaks.com/topic/261560-beginner-question-form/#findComment-1340315 Share on other sites More sharing options...
creata.physics Posted April 25, 2012 Share Posted April 25, 2012 I think you mean: header("Location: $url"); Quote Link to comment https://forums.phpfreaks.com/topic/261560-beginner-question-form/#findComment-1340318 Share on other sites More sharing options...
jcanker Posted April 25, 2012 Share Posted April 25, 2012 You're right, I did Rushed it without proofreading. Thanks for the catch. Quote Link to comment https://forums.phpfreaks.com/topic/261560-beginner-question-form/#findComment-1340381 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.