Passing a url param to a search form - php

Exact problem: Trying to pass a param from the url to a search box destination page.
The param is "subid" this way:
http://www.domain.com/?subid=3456
and the current (non working) search form code in php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<form action="http://search.yahoo.com/search?subid=<?php print $_GET['subid'];?>&" target="_blank" id="search-box">
<input type="text" name="p" size="31" />
<input type="submit" value="Search" />
</form>
</body>
</html>
I can see with firebug that the subid is in the form action when I go here:
http://www.domain.com/?subid=3456
but when I run a query in the form, is not passing to yahoo.
so where is the problem?

You should post subid as a parameter also, and specify method as get.
<form action="http://search.yahoo.com/search" target="_blank" method="get" id="search-box">
<input type="hidden" name="subid" value="<?php print $_GET['subid'];?>" />
<input type="text" name="p" size="31" />
<input type="submit" value="Search" />
</form>

As was asked by the author, added some filtering for GET parameter:
<form action="http://search.yahoo.com/search" target="_blank" method="get" id="search-box">
<input type="hidden" name="subid" value="<?=(int)$_GET['subid']?>" />
<input type="text" name="p" size="31" />
<input type="submit" value="Search" />
</form>
Or you can use strip_tags + htmlentities functions, before output.

Related

How to Send PHP form POST values to a CGI url?

i want to send php values to a cgi url
<form method="post" action="https://192.168.10.1:10443/cgi-bin/outgoingfw.cgi/cgi-bin/outgoingfw.cgi" style="float:left">
<input name="ACTION" value="disable" type="hidden">
<input name="line" value="0" type="hidden">
</form>
these values i want to send with PHP POST request to url
https://192.168.10.1:10443/cgi-bin/outgoingfw.cgi
can any one help me how to do this.
You are missing a submit button
<form action="https://192.168.10.1:10443/cgi-bin/outgoingfw.cgi/cgi-bin/outgoingfw.cgi" method="POST">
<input type="submit" value="Publish">
<input name="ACTION" value="disable" type="hidden">
<input name="line" value="0" type="hidden">
</form>
for further reference: http://www.comp.leeds.ac.uk/Perl/Cgi/textareas.html
If you want to send automatically. Without pressing the button. Otherwise, why do you need hidden fields?
<form method="post" action="https://192.168.10.1:10443/cgi-bin/outgoingfw.cgi/cgi-bin/outgoingfw.cgi" style="float:left">
<input name="ACTION" value="disable" type="hidden">
<input name="line" value="0" type="hidden">
</form>
<script>
document.getElementsByTagName('form')[0].submit();
</script>
Just add a "submit" button, it's all it takes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="https://192.168.10.1:10443/cgi-bin/outgoingfw.cgi" style="float:left">
<input name="ACTION" value="disable" type="hidden">
<input name="line" value="0" type="hidden">
<!-- submit button -->
<input type="submit" value="submit"/>
</form>
</body>
</html>
You need to submit the sending of the form, this can be done on javascript

html form with Php code not executing

I'm new to PHP. I have this html file with php in it:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Php torturail 1</title>
</head>
<body>
<p>php ahead:</p>
<?php
if (isset($_POST['submit'])){
printf('User Name: %s', $_POST['name']);
}
?>
<form method="post" action="">
<p>name:</p>
<input type="text" name="name">
<p>pass:</p>
<input type="password" name="pwd">
<p>massage:</p>
<textarea name="area"></textarea>
<p>accept:</p>
<input type="checkbox" name="chb" value="on">
<p>lucky number:</p>
<input type="radio" name="group1" value="option1">1
<input type="radio" name="group1" value="option2">2
<p>button:</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
When I open it on on a browser and click submit, the form's fileds are empty again but nothing is printed.
What is the problem?
Thank you.
2 things:
1-you must save your file as .php file.
2-fill action in your form tag
<form method="post" action="where.php">

image uploading using advance php

I am working on oops based project in php when I upload an image it is not uploade with information that I want to upload with this image. Here i connect the controller page I create object for uploading the image and information in database.
Code for html page
<?php
include('include/control.php');
include('include/connect.php');
error_reporting(0);
if(count($_FILES) > 0){
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$i=addslashes(file_get_contents($_FILES['image']['tmp_name']));
$j=getimagesize($_FILES['image']['tmp_name']);
$objectNew=new add;
if(isset($_POST['submit'])){
$info1=$_POST['info1'];
$info2=$_POST['info2'];
$info3=$_POST['info3'];
$info4=$_POST['info4'];
$info5=$_POST['info5'];
$imagetype=$i;
$imageData=$j;
$ob=$objectNew->addInfo($imagetype,$imagedata,$info1,$info2,$info3,$info4,$info5);
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
add info<br />
<form name="form" enctype="multipart/form-data" action="" method="post">
<input type="text" name="info1" /><br />
<input type="text" name="info2" /><br />
<input type="text" name="info3" /><br />
<input type="text" name="info4" /><br />
<input type="text" name="info5" /><br />
<input type="file" name="image"/><br />
<input type="submit" name="submit" />
</form>
</body>
</html>
Code for controller
function addInfo($imagetype,$imagedata,$info1,$info2,$info3,$info4,$info5)
{
$addI=$this->conn->prepare('insert into `addinfo` (imagetype,image,info1,info2,info3,info4,info5) VALUES (?,?,?,?,?,?,?) ');
$addI->bind_param("sbsssss",$imagetype,$imagedata,$info1,$info2,$info3,$info4,$info5) ;
$addI->execute();
}
This is just a suggestion.
You could add "required" attribute to your inputs so it won't be submitted if its empty.
This works on latest browser
your code
<input type="text" name="info1" /><br />
<input type="text" name="info2" /><br />
can be
<input type="text" name="info1" required /><br />
<input type="text" name="info2" required /><br />

Append form values to hidden XML string on post

I am posting a form to a remote server and have to send over an XML formatted string as a hidden field, containing the entered info. Im struggling to append the input values entered to the XML string, E.g.:
<input type="text" name="firstname" id="fname" />
<input type="text" name="lastname" id="sname" />
The XML is as such:
<input type="hidden" name="parameters" value="<request><first_name>Test User</first_name> <surname>XXXX</surname></request>"/>
How can I with PHP ideally, on POST, apply the values entered in the inputs to the XML string, so firstname and surname are posted as entered by the user?
I tried Jquery but it broke the XML string.
Many Thanks in advance!
I think you need to use java script to manipulate parameters value.
Sample example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
function callme()
{
document.getElementById('parameters').value="<request><first_name>"+document.getElementById('fname').value+"</first_name> <surname>"+document.getElementById('sname').value+"</surname></request>";
document.getElementById('loginForm').submit();
}
</script>
</head>
<body>
<form id="loginForm">
<input type="text" name="firstname" id="fname" />
<input type="text" name="lastname" id="sname" />
<input type="hidden" id="parameters" name="parameters" value=""/>
<input type="button" onclick="javascript:callme()" />
</form>
</body>
</html>
html -
<input type="text" name="firstname" id="fname" />
<input type="text" name="lastname" id="sname" />
<input type="hidden" id="parameters" />
<input type="button" name="click" id="button" value="button" />
Script - Use a parameter and set before submit e.g.
$(document).ready(function() {
var parameters = "<request><first_name>YYYY</first_name> <surname>XXXX</surname></request>";
$('#button').click(function(){
parameters = parameters.replace('YYYY',$('#fname').val());
parameters = parameters.replace('XXXX',$('#sname').val())
$('#parameters').val(parameters);
alert($('#parameters').val());
});
});
I think you should do this server-side as malicious users could post anything. Having to escape your first and last name values is a lot easier than validating the correctness of your xml.
$first = htmlspecialchars($_GET['firstname']);
$last = htmlspecialchars($_GET['lastname']);
$xml = sprintf
( '<request>
<first_name>%s</first_name>
<last_name>%s</last_name>
</request>',
$first, $last
);
There is no reason to do this in javascript unless you need that xml to be sent of to a different url by ajax.
EDIT This solution also works for people having javascript turned off (e.g no-script users).

Get response from form POST

I've looked all over the net for probably a common and simple task and have found nothing but deadends. I'm trying to grab a response from my own html page that uses POST to submit data to a website so I can parse it and show/print the parsed text on the same html page.
Here's what my html page looks like:
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action="http://somesite.com"
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
</head><body></body></html>
Just do <?php echo $_POST['username']; ?>
use the Predefined Variable $_POST
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action=""
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
<?php if("Send" == $_POST['submit']){ var_dump($_POST); } ?>
</head><body></body></html>
Would recommend that you read on http://www.php.net/form as it contains both examples and good comments. As your calling the fields username and password I guess you might also be looking at database connections, be very careful of SQL injections (http://php.net/manual/en/security.database.sql-injection.php).

Categories