Javascript messing up POST-request for PHP-file? - php

I got following scenario:
A site is sending a request to a php-file to hand me some data. For this request I am selecting an item - here is the code for that part:
<form action="?modul=transaktionen&subModul=monitor" method="post">
<input type="hidden" name="suchVal" value="1">
<input type="hidden" name="action" value="1">
<!-- A LOT OF STUFF INBETWEEN ... -->
<table>
<tr>
<td>
<input type="radio"
name="hostsARR[host][idGcomp]"
id="nod_331"
value="331">
</td>
<td>Some text which is really not important</td>
</tr>
<tr>
<td>
<input type="radio"
name="hostsARR[host][idGcomp]"
id="nod_332"
value="332">
</td>
<td>more text that is not important</td>
</tr>
</table>
<input type="submit" class="sendButton" name="edit" value="Show details">
</form>
And when I select one of these item and hit the button it should send me the request with these attributes:
action 1
edit Show details
hostsARR[host][idGcomp] 332
It does that normally, but when I add javascript to it it gets totally messed up! It does not send the right request. What happens is that I do not get the hostsARR. Everything else gets through. I added a script by frequency-decoder.com for pagination and for sorting. You can find the script here if you need a look at it: http://www.frequency-decoder.com/2007/10/19/client-side-table-pagination-script.
Basically my question is whether there are known Javascripts or bugs or whatever I don't know or am not capable of giving a name for that mess up POST (or other) requests?
Seriously this is driving me crazy as I really do not see a reason why sorting a table or rather adding javascript should alter a form element to that extent.
Thnx in advance for your help.
EDIT: The request doesn't work anywhere BUT IE ... dunno where IE is maybe more tolerant?

Answering to the information given, I can conclude that the pagination or sorting script has nothing to do with what causes your error.
My guess is that your manual actions have caused this or some other included script.
But unless you provide us these no-one can tell.
If you could provide the full source to recreate the problem we could all be of more help.

Related

$_GET method sporadically not passing any data.

so I have been building a small website with a content management system I have built using PHP and mysql. What I am trying to achieve is the user needs to be able to edit and delete the announcement posts they make, so I have been using $_GET to get the ID of the post that is to be edited/ deleted and chucked into a form which holds the code to do that.
Now, this is the code I have been using
if(isset($_GET['edit'])) {
header('Location: announcement-edit.php?AnnouncementID='.$_GET['AnnouncementID']);
}
The form later down the page to execute this is as follows
<form action="announcements.php" method="get">
<input type="hidden" name="AnnouncementID" value="<?php echo $row['AnnouncementID'];?>" />
<input name="edit" value="Modify" type="submit">
<input name="delete" value="Delete" type="submit" >
</form>
This is where it confuses me, this code worked absolutely perfectly then all of a sudden last week it stopped, 2 days ago it started working again and now broken again. When the button is clicked the URL returns as;
url/folder/announcements.php?AnnouncementID=7&edit=Modify
When its supposed to return;
url/folder/announcement-edit.php?AnnouncementID=7
If i manually type the above in the url bar it works absolutely fine and I can update the rows in the DB.
Im totally stumped as to why it is so temperamental, Have I missed something out? am I trying to do this an out dated way?
Its not like I have just followed a random person on youtube how to do this I was actually shown this way at university (only last academic year) and we was told it was "good practice". Seems very weird it works perfectly one minute then refuses to work the next.
Any thoughts or suggestions will be greatly appreciated.
You have two submit buttons in your form, which leads to triggering the submit event only once, when you press Enter key. So, when the Delete button is pressed, your GET parameters will have only delete entry and not the edit entry.
One way of tackling this is, give the same name to both and give a different value, as already there is a different value.
<form action="announcements.php" method="get">
<input type="hidden" name="AnnouncementID" value="<?php echo $row['AnnouncementID'];?>" />
<input name="action" value="Modify" type="submit" />
<input name="action" value="Delete" type="submit" />
</form>
You dont need to use form. You can do it simply using url and use tag
Try to pass your parametr via get parametr like this:
Edit
Delete

form session data not posting, sessions working

I've been tinkering with this for a few days. Checked out all the posts related and still can't quite get it to work. Anyone able to take a look and steer me in the right direction?
I have a site where everything gets loaded into a div on a page.
everything is working great except the form data.
I can load the session data for a test and it works fine, however i can't seem to get any of the entered form data.
i have the session_start(); included at the top just under the opening php tag in connection.php. connection.php is required on each page.
connection.php beginning.
<?
session_start();
Here are some code samples from addvehicle.php (not posting it all as it's huge and the rest is not relevant, can if need be though)
...
<script type="text/javascript">
$(document).ready(function()
{
$('form#newvehicleform').submit(function()
{
<?php
$trucknumbera=$_POST["trucknumber"];
$_SESSION['trucknumber']=$trucknumbera;
$_SESSION['info']="this is some info";
?>
$("#mainwindow").load("addvehiclepost.php");
return false;
});
});
</script>
.....
<form id="newvehicleform" class="newvehicleform" method="post" action="<?php echo $PHP_SELF;?>">
<table>
<tr>
<td>
Truck Number
</td>
<td>
<input name="trucknumber" class="validate[required,custom[number]]" type="text" id="trucknumber" autofocus autocomplete="off" required />
</td>
</tr>
<tr>
<td>
<br>
<input type="submit" id="submit" class="s-submit" alt="Submit" value="Submit">
</td>
</tr>
</table>
</form>
the test of, addvehiclepost.php
require ('./connection.php');
echo "<br>info: " .$_SESSION['info']. "<br>";
$trucknumber=$_SESSION['trucknumber'];
echo "<br>trucknumber: " .$trucknumber;
and here is the output i get from addvehiclepost.php
info: this is some info
trucknummer:
so you can see it's passing the $_SESSION properly and that function is working however I just can't get it to pass the submitted for data.
Anyone have any ideas on getting this to go?
Thanks in advance.
-Colin.
When you execute your first script with PHP, there is no form data.
And the PHP fragment executed there does not do anything besides transfering an empty POST value (NULL) to a local variable and then to the session array.
I think you think that this PHP code gets executed when the javascript is called, but this is not the case! First all PHP is executed, and the result is echoed back to the browser. Then the page is rendered and javascript is executed.
So the PHP part of your code does copy an empty value (and sets a static one as well, that's what you see) at the time the form page is first loaded by the browser.
Then javascript acts. Which would mean in your case that there is no form POST request, but simply a GET request for the addvehiclepost.php script. No POST request means no $_POST data. Which isn't accessed in the loaded script anyway.
I don't know if you are able to fix this, but I hope I enabled you to understand the structural error of your code. I have seen this with about any newbie I know.

Image buttons not working on internet explorer [duplicate]

When a form has multiple image inputs and the server side uses their names and/or values to distinguish which one was clicked, it works perfectly in FireFox. However, people often write the whole thing before finding out that HTML specifies that nothing has to be sent, and thus some browsers are not sending it.
It's not about sending any random object, but sending a pair as input_name=input_value. The best worst-case scenario example here would be what I've encountered: A list of elements all in one form and all accompanied by buttons with name="delete" value="<item_id>"
What can I do to fix this problem?
Per the HTML spec, clicking on an IMAGE input will return the parameters:
name.x=x-value and name.y=y-value where "name" is the value of the name attribute
with x-value and y-value corresponding to the click position.
Sure, the server code to deal with this will be a little annoying, but you could just check all the query parameter keys with a regular expression:
/^(.*)\.[xy]$/
to search for the IMAGE input keys to determine which IMAGE was clicked.
I tried with this sample:
<form action="#" method="GET">
<input type="text" name="t" value="Text here"><br>
<input type="image" name="a" value="1" src="http://sstatic.net/so/img/logo.png"><br>
<input type="image" name="b" value="2" src="http://www.gravatar.com/avatar/c541838c5795886fd1b264330b305a1d?s=32&d=identicon&r=PG"><br>
</form>
And I get the following urls:
FF 3.6: x.html?t=Text+here&b.x=19&b.y=17&b=2#
IE 8: x.html?t=Text+here&b.x=22&b.y=18
IE 7: x.html?t=Text+here&a.x=185&a.y=51
Opera 10: x.html?t=Text+here&a.x=107&a.y=53#
Chrome: x.html?t=Text+here&b.x=20&b.y=17&b=2#
So it seems that all the browsers are sending something image related, even if it isn't the image name directly. Since you need to scan for all the image names that you expect to see you can just scan for imagename.x instead. This seems to be how the spec indicates it should work.
The problem was half solved up to now: like here
But it didn't allow to get the value!
The correct answer is:
$('input[type=image]')
.unbind('mousedown')
.mousedown(function(){
$(this).after('<input type="hidden" name="'+$(this).attr('name')+'" value="'+$(this).attr('value')+'" />');
});
This code creates a hidden duplicate of the input when user starts clicking it. The unbind('mousedown') is to secure it happens once even if You put the code in multiple places in a weird application and it might be called more than once.
I recommend putting it in $(document).ready();
I think I am/was having a similar problem. I wanted to click on an thumbnail and have it enlarged on a different page. I was trying to do this with PHP alone but I finally had to use the tag with the . Worked great for FF3 and safari but the INPUT IMAGE values did not post for IE9 or FF9.
My work around was to put each image in its own form and then also use a hidden input to send the needed data.
<table>
<tr>
<td>
<form method="post" class="form_photo">
<input type="image" name="img_photo" value="does nothing in IE9 or FF9" />
<input type="hidden" name="photo" value="nameoftheimage.jpg" />
</form>
<form method="post" class="form_photo">
<input ...>
<input ...>
</form>
<form> ...
</td>
</tr>
Then I discovered the forms displayed vertical, making it very odd. CSS to the rescue.
.form_photo { display:inline; }
seems to have solved the vertical problem. Now the user can click on the thumbnail and the value now passes in all the browsers I have access to testing.
Using the type="image" is problematic because the ability to pass a value is disabled for some stupid lack of reason. Anyways & although it's not as customizable & thus as pretty, you can still use you images so long as they are part of a type="button".
<button type="submit" name="someName" value="someValue"><img src="someImage.png" alt="SomeAlternateText"></button>

How to handle form data by calling a PHP function in jQuery Mobile using Ajax?

I am currently working no a 6-page form using jQuery Mobile and are having some problems handling my form results. What I want to do is get all data sent to a .php script so the .php script can print and/or save to my database.
I've often had problems with everchanging variable names as they seem like a pain to manipulate. Perhaps someone know a better way to do this as I am pretty new to jQuery and Ajax. I have attempted to explain how my thought process is underneath.
Has anyone done anything similar that can show me in the right direction? I am pretty much in the dark here as what to google. Perhaps a link to something relevant? I would post any code if I had some, but I haven't really found anything useful to try yet. My form is generated through a .php function however, but saving the results is sort of the opposite of that.
Demands:
- No page reload
- Having the possibility to use mysql_real_escape_string before saving to db
- Submit method: POST
- Target db table needs question_id, option_id, user_id, date and other (for possible comments)
HTML pseudo code (generated by .PHP) - Without spans/fieldsets/labels etc
<form action="<!-- What to put here?-->" method="POST" id="questions">
<input type="radio" name="1[]" value="1"/>
<input type="radio" name="1[]" value="2"/>
<input type="radio" name="1[]" value="3"/>
<input type="checkbox" name="2[]" value="1"/>
<input type="checkbox" name="2[]" value="2"/>
<input type="checkbox" name="2[]" value="3"/>
<input type="checkbox" name="2[]" value="4"/>
<textarea name="2[]" placeholder="Type here..."></textarea>
<!-- ... -->
<a type="submit" href="#results" onclick="formSubmit()" data-role="button">Submit</a>
</form>
JavaScript Code:
function formSubmit()
{
document.getElementById("questions").submit();
}
Desired output in .php:
$answers = ("questionid" => "answerid", "questionid" => "answerid", "questionid => "answertext") for all checked checkboxes/radiobuttons and textfields != "".
Edit:
I've found out I probably need an id for my form so it shall hereby be named as questions.
The best way to do it would perhaps be by calling the .php script with an onclick on the submit button.
It sounds like you need help doing a simple AJAX submission but maybe there's some hidden question in here I've missed but I'll answer anyway.
So what you want is to look at this tutorial here http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
You set your URL to AJAX submit to a handler PHP script which will then do what you want with the data and return a status, success/failed etc.
Hope that helps.

Button type submit not working in IE6.0

Hallo Friends,
I need a button with text on it in 2 lines. The length of the text is more than the space i have for it.:-) Image is not of my preference.
I have used the following code
<tr>
<td align="center">
<button type="submit" name="add_invoice" id="buttonstyle">
Add to Ready for<br />
Invoicing
</button>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" name="delete" value="Delete">
</td>
</tr>
This is working pretty good in IE 7.0.
But in IE 6.0 eventhough i click the second button delete, in $_POST, it is returning the values of both the buttons.
it has the value of [add_invoice] and [delete]. So i am not able to figure it out which one is clicked (in IE 6.0 Only).
Please help me to understand this behaviour. Let me know if you know any other alternative.
Thanks in advance!
The <button> element is buggy in IE. Try using an <input type="submit"> ?
EDIT: Since you need it to span 2 lines, I suggest styling the input submit or creating an image, unfortunately.
EDIT #2: You can also probably use <!--[if IE 6]> conditionals to feed an input type='image' to IE6 and button to other browsers if it really matters.
I do not know if this would work or not, but you could probably use a javascript onClick event to trigger the submit.
<tr>
<td align="center">
<button type="submit" onClick="document.formname.submit();" name="add_invoice" id="buttonstyle">
Add to Ready for<br />
Invoicing
</button>
</td>
</tr>
Remember to replace formname with the actual formname if this is to work, which I do not know if it will, as I do not have IE6 setup, I cannot test it.
UPDATE
I am not sure if this would be worth it or not, but you can set it up like this (or in a function) to have the value be changed.
<tr>
<td align="center">
<button type="submit" onClick="document.formname.delete.value = ''; document.formname.submit();" name="add_invoice" id="buttonstyle">
Add to Ready for<br />
Invoicing
</button>
</td>
Which should set the delete value to be empty. It is hackish and not sure if I would do it this way, but it is a way to achieve it. I would probably store that in a function, however, and just call the function. For demonstration / testing reasons, I did not do that for this example.

Categories