Form Submit - Prevalidation throws IE off - php

Background: The website I am working on has a search bar at the top. The user inputs a part code for a product and the website returns information about that product.
So, I have a basic search bar that posts parameters from a HTML form into a PHP script, which then does a lookup on a MySQL server to get the Product.
The problem is because some part codes have "#" characters, I have to use Javascript to insert escape characters, otherwise I only get some of the part code in the PHP script.
Example - 123#ABC would be read as 123.
I use a hidden value in the form, which is populated with the Text Box value, modified by the escape() function in Javascript.
This is my code currently, it works in every browser except for IE.
Any help would be much appreciated :)
<script language="jscript">
function changeTextBox()
{
hiddenSearch.value = escape(txtSearch.value);
formSearch.submit;
}
</script>
<form id="formSearch" name="Search" action="?page=search" method="post">
Search by <u>Part Code</u> or <u>Description</u>
<input id="txtSearch" type="text" size="35">
<input id="hiddenSearch" name="Search" type="hidden">
<input name="Submit" onclick='jscript:changeTextBox();' type="submit" value="Submit">
</form>

take a look at changes-- i think there were issues with your javascript.
<script language="jscript">
function changeTextBox()
{
document.getElementById("hiddenSearch").value = escape(document.getElementById("txtSearch").value);
this.submit();
}
</script>
<form id="formSearch" name="Search" action="?page=search" method="post" onsubmit='changeText()'>
Search by <u>Part Code</u> or <u>Description</u>
<input id="txtSearch" type="text" size="35">
<input id="hiddenSearch" name="Search" type="hidden">
<input name="Submit" type="submit" value="Submit">
</form>

Related

HTML form not changing URL as expected

I have a problem in my project that i cant fix yet.
Im working with MySQL - PHP and everything is working OK but when I try to open "php/consult.php?consult=add" using the form below.
<form action="php/consult.php?consult=add" method="get">
<td>Instruccion SQL:</td>
<td>
<input type="text" name="codecosult" required/>
</td>
</form>
My browser doesn't change the URL to "php/consult.php?consult=add". Instead it shows only "php/consult.php", what have I done wrong?
Thanks for your answers and your time, and sorry for my english (it isn't too good xD).
This works if submitted - you don't need to add the query string - if it were an <a href="... then you would need the full query string for the url:
<form action="php/test.htm" method="get">
<td>Instruccion SQL:</td>
<td>
<input type="text" name="codecosult" required/>
</td>
<input type="submit" name="submit" value="Submit" />
</form>
This gives me my url + /php/test.htm?codecosult=ghrth&submit=Submit
ghrth was what I had typed.
If you are submitting it with a script leave the query string off and let the "GET" method deal with it - script here is in the head.
<script language="javascript">function submit_this(){this.form.submit();}</script>
</head>
<body>
<form name="form" id="form" action="php/test.htm" method="GET">
<td>Instruccion SQL:</td>
<td>
<input type="text" name="codecosult" id="codecosult" required/>
</td>
<div onClick="submit_this();">Submit</div>
This gives me my url + test.htm?codecosult=dfthwrth
dfthwrth was what I had typed.
This also works, though the <a href="...'s hrefis modified here by the keypress event each time a key is pressed (may not work in all browsers):
<input type="text" name="codecosult" onKeyPress="ahreff.href='php/test.htm?'+this.value;" required/>
Click Here
This might be of interest - a dropdown will pass its value in the same way:
dropdown menu doesn't function - it will now!

Need to pass a variable in anchor tag

I am very new to php, I am having a problem passing data from textbox to a php variable to use it in an anchor tag. Below is the code i am using.
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
I want to pass value from searchbutton to anchor tag
Hello"
If you are using jquery in your project and want to do this on the front end, you can do:
<a id="someLink" href="http://www.example.com/?q=var" target="_blank">Hello"</a>
$('#someLink').attr('href', "http://www.example.com/?q=" + $('q').val());
With php you'd only be able to set the entered value of q on a post. (meaning when someone submits the form)
i.e.
Hello"
IF you need to populate the link href without a page refresh, you'll need to use javascript, if you want it to be populated after a form post, you can use php.
Be aware though that the link would need to be on the page set in your forms action attribute to populate the link
You should be aware that you take precautions when echoing out form submissions, however the level of questions suggests you've got more to learn before that. (No offense intended)
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
On your fetchvalues.php page
<?php
$val = $_GET['q'];
?>
then
Hello
You can do like this
<?php
if(isset($_REQUEST['searchbutton']))
{
?>
Hello
<?php
}
?>

Use a PHP Function with a parameter from an HTML post form?

My question is this:
How do I use a php function with an argument FROM MY php/html form?
I downloaded this php function script from intechgrity.
Link: http://www.intechgrity.com/?p=808
The function is: itg_fetch_image('http://the.image.url/pic.bmp')
What I'm doing:
1) On my website I have a php page.
2) What I did was copy all of the intechgrity php script and pasted it into my page (at the top, of course)
In my page I have this form, but nothing is happening.
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="text" id="Stuff" name="Stuff" maxlength="30" value="<?=$img_url;?>" />
<button type="button" onClick="newSrc();">Do it!</button>
</form>
What am I doing wrong, how come when I hit submit the function is not working?
Something like this, maybe? (I didn't check through all the code you linked to.)
<?php
function itg_fetch_img($img_url, ...) {
etc.
}
$img_url = $_POST['Stuff'];
if (!empty($img_url)) itg_fetch_img($img_url, ...);
?>
<form action="" method="post">
<input type="text" id="Stuff" name="Stuff" maxlength="30" value="<?=$img_url;?>" />
<input type="submit">Do it!</button>
</form>
Note that button is now an input with type submit, and javascript function is removed.

Get Search keyword in URL

<form action="">
<input placeholder="SEARCH" name="search_input" type="text"/>
<input type="submit" name="search_submit"/>
</form>
If people search by "Keyword Item" I want URL will be http://mydomain.com/search?keywords=Keyword%20Item
How can I do it? I know needs configure in form action, get etc.
Thanks in advance.
Update
When I am trying with this code
<form action="http://search.golfoutletsusa.com/search?" method="get">
<input placeholder="SEARCH" name="Keywords" type="text"/>
<input type="submit" name="search_submit"/>
</form>
The URL is: http://search.golfoutletsusa.com/search?Keywords=85&search_submit=Submit+Query
I just want "&search_submit=Submit+Query" will be removed from URL.
<?php echo $_GET["keywords"]; ?>
You will need to change the name of the text field from search_input to keywords though.
You should also consider using an id attribute along with the name. And as the other answer says, form action and method should be set correctly.
Solution:1
You can add the following code at the top of your search.php (or, whatever the processor file):
<?php
if(isset($_GET["search_submit"]))
{
$keywords = $_GET["Keywords"];
header("Location: search.php?Keywords=$keywords");
}
?>
OR
Solution:2
You can omit the name of submit button if it is not really necessary to give it a name. So instead of
<input type="submit" name="search_submit"/>
just use
<input type="submit" />
Set action of the form to search.php and method to get.
Then change the name of your input element to keywords.
But still the url won't be - http://mydomain.com/search?keywords=Keyword%20Item
It will be - http://mydomain.com/search.php?keywords=Keyword%20Item
Simply put everything in the form properties, only you have to select a file which will receive all this, /search will not suffice:
<form action="search.php" method="get">
<input type="text" name="keywords" placeholder="SEARCH" />
<input type="submit" name="submit" />
</form>
EDIT: In the search.php file you would get the variable contents with the get global variable like this:
$search_query = $_GET['keywords'];
After that you simply write the rest of the code to do the search... Note that this would lead to an URL like http://www.example.com/search.php?keywords=query

send the form value into the form action parameter

Can i send a input value into form action ?Let say, on the form the phone number taken.Now can i send the phone number as form action parameter "number"? Is their any way to send it?
<form method="post" action="abc.php?number=ph_number" enctype="multipart/form-data">
<input type="text" name="ph_number" value=""/>
<input type="submit" name="search" value="SEND"/>
</form>
How can i do it?
Thanks in advance
riad
<form method="GET" action="abc.php" enctype="multipart/form-data">
<input type="text" name="number" value=""/>
<input type="submit" name="search" value="SEND"/>
</form>
Change action="abc.php?number=ph_number" to action="abc.php
Change name="ph_number" to name="number"
When you click submit, the value contained in "number" text field will be passed to abc.php.
Receive the value with $value = $_REQUEST['number']; in abc.php.
You can leave an empty action, and use the onSubmit event to load a javascript function that does whatever and redirects to the page according to the input value.
Html
<form .. action="" onsubmit="return abcByPhone(this);">
Javascript
function abcByPhone(form) {
url = from.number.value;
...
}
EDIT:
I actually didn't read the question properly. I thought you wanted to redirect to different pages according to the input. Using plain GET (like the others mentioned) is fine for this.

Categories