Change URL on submit form - forms

So I am trying to make a simple external popup that has 1 pull down menu for category and 4 columns of checkboxes as tags. (this is to feed a wordpress search engine plugin)
I use regular form submit for this but I end up with this as final URL:
Site.com/?category_name=VALUE&tag=TAG1&tag=TAG2&tag=TAG3
But I want my URL to be like this:
Site.com/?category_name=VALUE&tag=tag1+tag2+tag3
Could anyone point me in the right direction into achieving this?
CATEGORY_NAME is for searching a specific WordPress category
and the TAGS are regular post tags.

You can't do that with a checkbox. That is not how checkboxes work. It is not a choice, really. It the way that HTML, PHP, and your browser(s) are built.
Additionally, what you have done won't work. You've set the same variable to several different fields. Each time you set is, you overwrite the previous value. While your URL may look like ?category_name=VALUE&tag=TAG1&tag=TAG2&tag=TAG3 if you were to add var_dump($_GET); to your script you'd see that the only thing PHP sees is the last one-- tag=TAG3.
You can get the code working by using square brackets-- []-- in the checkbox names. An example in very minimal code (for demonstration purposes only):
echo '<form>';
echo '<input type="checkbox" name="tag[]" value="tag1">';
echo '<input type="checkbox" name="tag[]" value="tag2">';
echo '<input type="checkbox" name="tag[]" value="tag3">';
echo '<input type="checkbox" name="tag[]" value="tag4">';
echo '<input type="checkbox" name="tag[]" value="tag5">';
echo '<input type="checkbox" name="tag[]" value="tag6">';
echo '<input type="submit" value="Clickie">';
echo '</form>';
The 'tag' part of you URLs will look like this however:
&tag[]=tag1&tag[]=tag4&tag[]=tag6
That is just how checkboxes work. There are two workarounds that I can think of.
The first is to use Javascript to populate a hidden field in your form.
echo '<form>';
echo '<input type="hidden" name="tag" value="">';
echo '<input type="checkbox" value="tag1">';
echo '<input type="checkbox" value="tag2">';
echo '<input type="checkbox" value="tag3">';
echo '<input type="checkbox" value="tag4">';
echo '<input type="checkbox" value="tag5">';
echo '<input type="checkbox" value="tag6">';
echo '<input type="submit" value="Clickie">';
echo '</form>';
You would have your Javascript watch for clicks on those checkboxes and fill the value into the hidden tag field. In order to get the URL you want you have to remove the name from the checkboxes, which makes this form entirely dependent upon Javascript. Without Javascript it won't work. I consider that bad design.
The other option is to use mod_rewrite to try to rewrite your URL, but honestly, I don't know if mod_rewrite is capable of the complex regex you would need to make that work.
I don't think either workaround in worth the effort or the price. I would suggest you use the square brackets and process the array at the receiving end. In other words, if you have &tag[]=tag1&tag[]=tag4&tag[]=tag6 then this will get a string like what you want in your URL:
if (isset($_GET['tag'])) {
$tstr = implode('+',$_GET['tag']);
}
echo $tstr;
You can use that in your search function, whatever you are using for that.

This is what I did. Instead of adding a extra function to my wordpress (which was causing some unexpected error when testing deeper) I simply created a PHP to catch the URL before submiting further.
This is what it looks like:
<?php
if (isset($_GET['tag'])) {
$tstr = implode('+',$_GET['tag']);
}
$cat = ($_GET['category_name']);
header("Location: http://url.com/?category_name=$cat&tag=$tstr");
?>
I don't know if this is correct or secure, but it works :D
Thanks for your help !

Related

Getting HTML of a Cake PHP form

I am creating a form using Cake PHP. Is there any way to retrieve the basic HTML of the created form. For example,if we use Form Helper, we can create form using PHP itself. But now, I need only the html part of the created form for other use. Is it possible to retrieve it??
For example, say if I give input form like,
<?php
echo $this->Form->create('User');
echo $this->Form->input('email');
echo $this->Form->end('Save');
?>
I need output like this
<form action="index.html">
<input type="email" />
<input type="submit" value="Save" />
</form>
I can even create seperate function for attaining this objective. But I would like to know, if there is any other method for achieving this output
you can store FormHelper output in a string variable
<?php
$html_string = '';
$html_string .= $this->Form->create('User');
$html_string .= $this->Form->input('email');
$html_string .= $this->Form->end('Save');
?>
and then use your string elsewhere. But I'm not sure this is what you're searching for.
If I understand the question correctly, you want to use the form you created on another part of your site?
If that is the case, I would put the form itself in an Element and then call the Element wherever I wanted the form.
View/Elements/form.ctp
<?php
echo $this->Form->create('User');
echo $this->Form->input('email');
echo $this->Form->end('Save');
?>
Then in any view on your site you can call the Element using:
<?php echo $this->element('form');?>

How to prefill website form fields that are not posted

I don't really know exactly how to search for this on the web. I have a form with one input for telephone. Telephone is optional. I would like to have the word "(Optional)" in the form field. This I have done already like this:
<input type="text" name="billing[telephone]" value="<?php echo $this->__('(optional)') ?>" onFocus="if(this.value == '<?php echo $this->__('(optional)') ?>') {this.value = '';}" onBlur="if (this.value == '') {this.value = '<?php echo $this->__('(optional)') ?>';}" title="<?php echo $this->__('Telephone') ?>" class="input-text" id="billing:telephone" />
It works this way but it posts "(Optional)" everywhere, sends "(Optional)" out in customer emails etc. I would like to make it where if no phone number was inputted it doesn't post "(Optional)" and just leaves the field blank as if nothing was entered.
This code above is from Magento Shopping cart that I added the Optional code in.
Thank you
You could try using the placeholder parameter like so:
<input type="text" name="phone" placeholder="(###) ###-#### (optional)">
It will put in a temporary value to help the user, but will not be posted if the user does not input their own answer. It will also disappear on focus to allow the user to fill in their own answer without having to delete the "optional" text.
The placeholder technique is new in HTML5 :
<input type="text" name="phone" placeholder="(###) ###-#### (optional)">
I think this above line will not support browser compatibility

if isset doesn't work in php

I'm having a form with only one submit button. I don't know why, but when I use this code and I click on the submit button, nothing is happening. If I use a ! before the isset you'll see the echo in the page. I don't know what's wrong with it.
<form>
<input type="submit" value="Toevoegen" name="addImg" />
</form>
<?
if (isset($_POST['addImg'])) {echo "haaallloooo";}
?>
Maybe, form by default is sending variables by get, try using method="POST" attribute in form tag
You have to set the method to POST.
Otherwise you can use:
$_REQUEST['addImg']
The variable $_REQUEST can access both GET and POST parameters.
Form needs an action and a method.
<form action="" method="post">
<input type="submit" value="Toevoegen" name="addImg" />
</form>
<?
if (isset($_POST['addImg'])) {echo "haaallloooo";}
?>
Regarding "isset", if $_POST['addImg'] is not set, it doesn't echo "haaallloooo".
isset — Determine if a variable is set and is not NULL
Check http://hk.php.net/manual/en/function.isset.php

Symfony: form field access with no sfForm object

i'm working on a symfony project and i developed a form to upload a file and save its info to a table in my model. And didn't use the sfForm class to implement my form.
Here you have my form
<form name="new_file" action="<?php echo url_for('home/uploadFile');?>" method="post">
<input type="file" id="file">
<input value="<?php echo $codigo_maestro?>" id="master_id">
<input value="<?php echo $codigo_entidad?>" id="entity_id">
<input type="submit" value="Upload">
</form>
So now i'm trying to access the fields of the sumbited form in my action function and don't know how :(
$request->getParameter('file');
$request->getParameter('master_id');
$request->getParameter('entity_id');
this code didn't work.
So please help me solve this! How can i access the fields of my form from the action??
You need to add a name to your form fields, that's the name you can access them via $request->getParameter().
finally i did this way, maybe not most elegant, but working
in action.class.php
$file= $_FILES['file'];
$filesize = $archivo['size'];
$filetype = $archivo['type'];
$filename = str_replace(' ','-',$file['name']);

Zend controller/view newbie puzzle: $_GET & $_POST empty - on receipt from HTML form within view

Zend newbie here ... And just to make it better, my mission is to build on top of someone else's pre-existing Zend site.
(BTW: zf show version --> Zend Framework Version: 1.11.1 -- I seem to have Zend_Form).
Here's the curious bit. All the forms are built in HTML within views. They seem to work, although I can't figure out how -- especially given what I am seeing.
I followed the convention and created a view for a test form and wrote the form:
<form action="<?php echo $this->url(array('controller'=>'ControllerName','action'=>'submit'));?>" method="post" style="margin-left:20px">
<p class="bold setmgr">Your email here:</p>
<div class="field">
<input class="text" type="text name="custEmail"/>
</div>
<div class="field">
<input class="button" value="Submit and be free!" type="submit"/>
</div>
</form>
The submitAction member in the controller is firing correctly. No problem.
But ALL the places I could look for the POST data appear to be empty!
echo "obj custEmail = [" . $this->_request->getPost('custEmail') . "]\n";
echo "GET custEmail = [" . $_GET['custEmail'] . "]\n";
echo "POST custEmail = [" . $_POST['custEmail'] . "]\n";
if ($this->_request->isPost()) {
$data = $this->_request->getPost();
Zend_Debug::dump($data);
}
They all produce nothing.
I'd be much obliged for a solution or even a clue about what is going wrong.
Thanks for reading.
Your form is not in the correct format.As it's PHP you can use form like this or you can even generate a ZEND_FORM(which is profound way to do it).It's always a good practise to work around with ZEND_FORM.If you still want to use this and the go by your way,here is th snippet I modified for you.
I am modifying the Code for you.Your View should have this form in it;
<form action="" method="post" style="margin-left:20px">
<p class="bold setmgr">Your email here:</p>
<div class="field">
<input class="text" type="text" name="custEmail"/>
</div>
<div class="field">
<input class="button" value="Submit and be free!" type="submit" name="submit"/>
</div>
</form>
<?php
echo $this->custEmail;
?>
Now write the following one on your ACTIOn,i.e. submitAction;
public function submitAction()
{
if ($this->getRequest()->isPost())
{
$custEmail = $this->getRequest()->getPost('custEmail');
echo $custEmail;
$this->view->custEmail = $custEmail;
}
}
Now check if it works for you or not.
Create a form using Zend_Form. When ZF already has a way to create forms, you should use that. Your method is like a hack and is not a recommended way to do things.
Check here on how to create a Zend_Form
http://framework.zend.com/manual/en/zend.form.html