Jump to content

Beginner Question : form


boumbo

Recommended Posts

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>

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.