HTML
<link rel="stylesheet" type="text/css" href="<?php echo CSS_URL ?>modal.css" />
<form id="postinfo" action="" method="POST">
<input type="hidden" name="technician" value="<?php echo $technician; ?>">
<input type="hidden" name="custnum" value="<?php echo htmlentities(serialize($customerNum)); ?>">
<input type="hidden" name="meternum" value="<?php echo htmlentities(serialize($meterNumbers)); ?>">
<input type="hidden" name="remcred" value="<?php echo htmlentities(serialize($remCreditArray)); ?>">
<input type="hidden" name="visitdate" value="<?php echo htmlentities(serialize($dateArray)); ?>">
<a href ="<?php echo PROCESS_URL; ?>create_maint_token_excel.php" onclick="document.getElementById('postinfo').submit();">
<img src="<?php echo IMAGE_URL ?>Excel.png" name="Print Excel" title="Print Excel" alt="Print Excel" /></a>
</form>
PHP
$customerNum = unserialize($_POST['custnum']);
$meterNumbers = unserialize($_POST['meternum']);
$remCreditArray = unserialize($_POST['remcred']);
$dateArray = unserialize($_POST['visitdate']);
$technician = ($_POST['technician']);
I have tried numerous ways of posting these arrays to a php page but the page keeps telling me that custnum, meternum, remcred and visitdate are undefined.
I have tried using a form action and post, I have tried to use a piece of javascript and I keep getting the same problem, why is this not posting?
I have also tried using $_GET instead of $_POST on php page to no avail.
<a href ="<?php echo PROCESS_URL; ?>create_maint_token_excel.php" onclick="document.getElementById('postinfo').submit();">
This will not work, the submit will go into nirvana and you will be taken to the PHP page without the variables. You are doing two requests at once and your browser will go to the one without the form variables.
Try changing your form to this:
<form id="postinfo" action="<?php echo PROCESS_URL; ?>create_maint_token_excel.php" method="POST">
And your Link to this:
<a href ="#" onclick="document.getElementById('postinfo').submit();">
This should submit the form correctly without following the link.
Alternatively use a propper <input type="submit" /> button, you can add the image to it with CSS.
Why don't you just use a form submit button, rather than making life complax with javascript? But first.. Debug, check what is actually in your hidden input.
Better would probably be to keep all these details on the server in a table called maintenance, and only pass the record ID to the webpage. A lot less info that the user can tamper with.
Also, you can just use a regulaer weblink to link to load the next page with the variables in the url (read GET), unless there is some input from the user which requires a form, which we do not see.
You should be trying as,
<?php foreach ($customerNum as $item): ?>
<input type="hidden" name="custnum[]" value="<?php echo $item; ?>"/>
<?php endforeach ?>
In php you could get as,
$customerNum = $_POST['custnum'];
And use,
<a href ="javascript:void(0)" onclick="document.getElementById('postinfo').submit();">
<input type="hidden" name="technician" value="<?php echo $technician; ?>">
<input type="hidden" name="custnum" value="<?php echo htmlentities(serialize($customerNum)); ?>">
<input type="hidden" name="meternum" value="<?php echo htmlentities(serialize($meterNumbers)); ?>">
<input type="hidden" name="remcred" value="<?php echo htmlentities(serialize($remCreditArray)); ?>">
<input type="hidden" name="visitdate" value="<?php echo htmlentities(serialize($dateArray)); ?>">
<a href ="<?php echo PROCESS_URL; ?>create_maint_token_excel.php" onclick="document.getElementById('postinfo').submit();">
<img src="<?php echo IMAGE_URL ?>Excel.png" name="Print Excel" title="Print Excel" alt="Print Excel" /></a>
just replace to inpuit type submit or you can use type image and add action url to action tag of form then you would get values by post method like below exa
Here's a simplified test case:
<form id="postinfo" action="" method="post">
<input type="hidden" name="foo" value="1">
Submit
</form>
If you hit "Submit" you'll see that your browser will open the href URL before your JavaScript code has any chance to make a POST request to the action URL.
You'll possibly want one of these:
<!-- No action href -->
Submit
<!-- Cancel click event -->
Submit
<!-- No link at all -->
<div onclick="document.getElementById('postinfo').submit();">Submit</div>
Side note: If you use PHP serialization, beware of object injection.
Related
I'll try and explain this as best as I can. Basically, I'm using a form to receive a comment. Upon hitting submit, the action creates a link similar to this: http://localhost:8080/camagru/comment.php?comment=test&post=Post
I have a variable with the image name in it that I want to pass as well, so something like this: http://localhost:8080/camagru/comment.php?img=test.png&comment=test&post=Post
I've tried using <form action="<?php echo commentpost.php?img=$img?>"> But everytime the submit button is pressed, it erases the img variable from POST and only puts in the new variables from the form.
Any suggestions?
add new hidden field in form tag like that
<form action="commentpost.php" method="post">
<input type="hidden" value="<?php echo $img ?>" name="img" />
<input type="submit" value="Save" name="IsSubmit" />
</form>
Now you can able to use $_POST['img']
The img variable is in GET.
If you want it in POST, try <input type="hidden" name="img" value="test.png">
use quotes in your case:
<form action="<?php echo "commentpost.php?img=$img"; ?>">
the best practice is to insert hidden element into your form:
<input name="img1" type="hidden" value="test.png" />
<input name="img2" type="hidden" value="test2.png" />
I am new to codeigniter, I am trying to use url as the form submission in codeigniter. The home page is having a form submission with fields like city, date and submit. The url it redirects to is domain.com/HotelSearch/amsterdam/1 but if I use the link directly for example domain.com/HotelSearch/Paris/1 it will not open anything. How can I use the url to submit Paris as the city and redirect to the result page. In an another method when I use hidden input values in the form it also works like that.
<form method="post" action="<?php echo VPATH; ?>searchHotel/Singapore/1" style="margin: 0;">
<a href="javascript:void(0);" onclick="$(this).closest('form').submit()" >
<img src="<?php echo IMAGE; ?>hotl02.jpg" class="img-responsive" alt="Responsive image">
</a>
<input type="hidden" name="city" value="Singapore" />
<input type="hidden" name="checkindate" value="<?php echo $today; ?>" />
<input type="hidden" name="checkoutdate" value="<?php echo $posttoday; ?>" />
</form>
You'll have to edit the controller code.
The url is in segments. You can grab each segment using $this->uri->segment(n) where n is the segment.
for example: http://www.hotelsample.com/searchHotel/Singapore/1
$this->uri->segment(1) will give searchHotel
$this->uri->segment(2) will give Singapore.
With this you can swipe out the city and process.
Goodluck
I have a hyperlink as shown:
<div style="clear:both"> <a color="grey" accesskey=""style="float: right" href="newbattle.php? userid= <?php echo $id0; ?>"> [<font color="grey">Attack</font>]</a><br></div> <br>
Is it possible, using only only php, to carry POST data? I want to put this
<input type="hidden" name="test" value="<?php echo $number;?>
So I can $_POST['test'] on the other page and get the $number. I can switch over to normal form but I really like what I have
No, that's not possible. If you want to submit a POST request, you should go through a <form> and submit it.
You cannot post through a hyperlink, unless you use JavaScript to capture the click event and simulate a click on a submit button.
But a better approach, I think, would be to make an actual submit button. With a bit of CSS you can style that button to look as if it was a hyperlink. That way, if the CSS fails, you've still got a working button, while if a JavaScript issue would occur, you have a disfunctional link with unexpected behaviour.
input[type=submit] {
display: inline;
border: none;
background-color: transparent;
color: blue;
cursor: pointer;
}
input[type=submit]:hover {
text-decoration: underline;
}
<form action="otherpage.php" method="POST">
<input type="hidden" name="test" value="<?php echo $number;?>">
<input type="submit" value = "Look, I'm a link!">
</form>
A link redirects user to another page, it's purpose is not for get/post requests.
If you want to send a post request on a click, you can do it with a submit button inside form. For example,
<form action="another_page.php" method="POST">
<input type="hidden" name="test" value="<?php echo $number;?>
<input type="submit" />
</form>
Style the button like a hyperlink and it will send a post request as expected.
You can do that using a form.
When the user clicks the link, the form is submitted with the "test" variable and the "userid" variable.
Here's the code:
<form method="post" action="newbattle.php" id="myForm">
<div style="clear:both">
<a color="grey" accesskey="" style="float:right;" href="" onclick="javascript:document.myForm.submit(); return false;">[<font color="grey">Attack</font>]</a>
<br/>
</div>
<br/>
<input type="hidden" name="userid" value="<?php echo $id0; ?>" />
<input type="hidden" name="test" value="<?php echo $number; ?>" />
</form>
For my specific problem, here's what I ended up doing.
href="newbattle.php?userid= <?php echo $id0; ?>&num=<?php echo $number; ?>"
I added the $number on to the hyperlink and then retrieved it with $_GET on the next page
If you want to access test value by $_POST you have to use form like this :
<form action="another_page.php" method="POST">
<input type="hidden" name="test" value="<?php echo $number;?>
<input type="submit" />
</form>
get.php :
<?php
$num = $_POST['test'];
echo $num;
?>
my php configuration on server are showing i can post variables to maximum size upto 8MB , thats enough .. but how to check for number of variables , sever is running ubuntu 4.4 , php .
i have a page which takes students marks and send them to a action page , but on action page doing echo for the post variables nothing is being displayed , where are doing an echo "hello"; this shows ...
this is the page which sends the variables
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable() ?>
<div class="mainframe">
<input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
and this are the variables are coming to action page .. but on echo they are not showing any value .
$dept =$_COOKIE['dept'];
$join=$_POST['batch'];
$type='e';
$sem=$_POST['sem'];
$chance=$_POST['chance'];
try placing this code on your action page:
if (isset($_GET)) {
echo "<h3>GET METHOD</h3>";
var_dump($_GET);
}
if (isset($_POST)) {
echo "<h3>POST METHOD</h3>";
var_dump($_POST);
}
if (isset($_COOKIE)) {
echo "<h3>COOKIE METHOD</h3>";
var_dump($_COOKIE);
}
See which method returns your variables and use it, otherwise, you are not filling any values on the form.
this is your code:
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable(); ?>
<div class="mainframe"> <input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
One possible reason for your issue:
You use "_GET[]" variables here but the form is POST.
GET and POST are two different methods to send data, GET is in the URL path (a=&b=&c=) while POST is hidden in the HTML headers.
So make sure you read those results as "$_POST['name']" and not GET.
I suggest this in the "receiving script" for debugging:
var_dump($_GET);
var_dump($_POST);
And in your browser use Chrome or Firefox + Firebug and Press "f12".
In that debugger you can catch the POST when you click the button and you can look which variables were sent.
That should help you debug your issue fast.
One other suggestion, I personally would write the code less "mixed".
It makes it hard to read and hard to modify.
Why not like this:
<?php
echo "
<form name='frm' action='marklistI.php' method='POST' class='' >".
$tb->displayTable().
"<div class='mainframe'>
<input type='hidden' name='batch' value='$_GET[batch]'/>
<input type='hidden' name='sem' value='$_GET[sem]' />
<input type='hidden' name='chance' value='$_GET[chance]'/>
<input name='submit' type='submit' class='hide' value='Save'/>
<input type='hidden' name='url' value='$_SERVER[REQUEST_URI]'/>
</div>
</form> ";
?>
My guess for your problem is that those values in the formular are actually empty, that's why you don't receive anything.
How can I convert this $_GET method to $_POST method?
$linkword .= "\n$alpha[$c] ";
You cannot pass $_POST data in a url, you must use cURL (PHP), AJAX (javascript), or a similar tool to build full HTTP requests.
ALTERNATE SOLUTION
You could however build a small form that submits, but you would have to use a submit button control for the "link" and use some hidden form inputs. You can Re-style the button anyway you wish with CSS.
<form action="link/url.php" method="post">
<input type="hidden" name="letters" value="value of letters" />
<input type="hidden" name="n" value="value of $n" />
<input type="submit" value="text of button" name="submit" />
</form>
You cannot POST data using URL Parameters. You will have to use a form with a method set to POST. Then submit the form on clicking the link or use a button.
Solution would be to use hidden form elements for your params and submit the hidden form when your click on the anchor tag.
Another solution would be to use ajax to post data. If you use jQuery or some libraries, you can do so.
Instead of creating a anchor tag. Create a hidden form
$linkword .= "\n$alpha[$c]";
Here is a Sample
<form name="hiddenform" method="POST" action="page.php">
<input type="hidden" name="letters" value="<? echo alpha[$c].$letters; ?>" />
<input type="hidden" name="n" value="<? echo $n; ?>" />
<a onclick="document.forms['hiddenform'].submit();">Test Link <? echo $alpha[$c]; ?></a>
</form>
Jsfiddle Demo