Passing the value to the next page without a form - php

I have a page I am constructing and I need to pass in the values of the option dropdowns to the next page. The problem is that these dropdowns are not in a form.
http://posnation.com/test/pre_config/pre_config_step_2.html
Basically what i need to pass to the next page is that when i click "Proceed To Next Step" I need to pass the value of the type of field like "restaurant" and the number of stations "2" if the user selects restaurant and 2.

HTML:
<a id="proceed" href="foo.html">Proceed!</a>
JS:
$('#proceed').click(function() {
location.href = this.href +'?someVal='+ escape($('#my_select').val());
return false;
});
Working example that executes a formless google search: http://jsfiddle.net/CKcbU/
You basically just add what you want to the query string with javascript.
But really, if at all possible, you should use a form with method="get" which pretty much does this for you without any JavaScript at all.

Use a query string.
http://posnation.com/test/pre_config/pre_config_step_2.html?restaurant=The+Eatery&stations=2
In other words, pass them as part of the URL when calling the next page. The next page will be responsible for reading the query string and extracting the values out.
http://en.wikipedia.org/wiki/Query_string
I do not know what you are using to code in so I cannot be detailed regarding the mechanics of constructing the URL or parsing out the values from the query string on the receiving page.
Here is an article on doing it using JavaScript:
http://javascript.about.com/library/blqs.htm

You can use JavaScript for the same. Assign a onclick event with button of proceed and call a function. In this function use:
windows.location=url?rest=valuerest&opt=valueopt

I am not sure what you are working with, but with almost any framework that I know of you can pass a parameter as part of the url.
These would work fine.
http://posnation.com/test/pre_config/step_2
http://posnation.com/test/pre_config/step_3
Then, just grab the parameter and react accordingly.

Related

Session variable passing

I've got a php script which builds a html table via echoing data, But i want to add a link onto one of the values and pass that value to the next page.
<td><a href='redirect.php'><?php $_SESSION['WR'] = $row['WorkOrdRef'];echo $row['WorkOrdRef'];?></a></td>
is the line in question but this will only pass the last value added to the table.
Oh, it doesnt work like this. the php code gets executed no matter if you click the link.
I guess the easiest way to do this is to pass it as a get parameter.
html page:
<?=$cellContent?>
redirect.php:
$clickedcell = $_GET['clickedcell']
now the $clickedcell will have the data from the previous page about what cell did the user click.
If you want to use session for some reason, you still have to pass it with GET or POST and store it after the user clicks.
hopefully this is understandable and good luck with your project.
you can change the session by get method also it is possible building by javascript
in the same page add this
if(isset($_GET["clicked"])){
$_SESSION['WR'] = $row['WorkOrdRef'];
$redirect'<META HTTP-EQUIV="REFRESH" CONTENT="0;URL='.$adres.'/"> ';
return $redirect;
}
and then change your url
<td><?php echo $row['WorkOrdRef'];?></td>

onClick add to database

I have the below code, and I'm not sure if it even works. Basically, I want to do a like system, whereby when I click on the link, it adds a +1 to the user's likes.
I've tried so hard to read up but I just don't get anything, and I found a code similar below.
When I clicked on the link, it does process my insert.php page, but I don't know how to get the variable values...
What should I do? I'm not sure if the code structure below is correct...
Thanks!
<script>
function insertSalary()
{
var salary = $("#salary").val();
$.post('insert.php', {salary: salary}, function(data)
{
$("#current-salary").html(data);
});
}
</script>
<div id="current-salary">
<a id="salary" onClick="insertSalary();">+1</a>
</div>
The variable will be in your php script as $_POST['salary']
The value of salary is passed as part of the post method in jquery.
So you can do:
$.post('script.php', {salary: 100}, function(data){...});
and this will pass the value 100 to your php script as the salary value.
In php the $_POST and $_GET hashes contain the data that you pass with a given request. In jquery $.post, $.get $.ajax create requests and take data hashes to build the data you want to pass with the request.
While this may work, I would recommend separating your logic from your website by putting the javascript in an external file and then link your HTML page to it.
I would also advise against declarative event binding which you have done by specifying onClick="insertSalary()".
jQuery provides a method to pro-grammatically assign functions to events using the on method. So for your code, you could use:
$('#current-salary').on('click', insertSalary());

Submitting GET data with no input field?

I've never really thought about this, but it helps with some security of something I'm currently working on. Is it possible to submit GET data without an actual input field, and instead just getting it from the URL?
If so, how would I go about doing this? It kind of makes sense that it should be possible, but at the same time it makes no sense at all.
Perhaps I've been awake too long and need some rest. But I'd like to finish this project a bit more first, so any help you can offer would be appreciated. Thanks
Yes. If you add some query-string to yourl url, you can obtain that in php using $_GET without form submitting.
Going to this URL adress http://yoururl/test.php?foo=bar cause echoing foo (if there will be no foo query string, you'll get warning).
# test.php
echo $_GET['foo'] # => bar
Is this what you mean?
Link
// page.php
echo $_GET['type']; // foobar
This is what I understand of your question:
You have a <form method="get" action="foo.php">-like tag on your page
You have a series of <input type="text" name="bar"/> in your page
You want to pass additional GET parameters that are not based on an input from the form
If so, it is possible, but I hardly see how it could help with security. Input from a client cannot be trusted, so even if you hardcode the GET value, you have to check it serverside against SQL injection, HTML injection/XSS, and whatnot.
You have two ways:
Use a hidden input: <input type="hidden" name="myHiddenGetValue" value="foobar"/>
Add the GET parameter to the form action: <form method="get" action="foo.php?myHardcodedGetValue=foobar">
If what you meant is that you want to have a GET request without a form, you just need to pass all the GET parameters to the href of a link:
Click here!
Yes it's possible. Just append the GET data to the link.
For example:
<a href="main.htm?testGet=1&pageNo=54>Test</a>
You can also use Javascript to build the url.
If you happen to be using jQuery and want to build the GET data dynamically you can do this:
var getParams = { testGet:1, pageNo:54 };
$(".myLink").attr("href", url + "?" + $.param(getParams));

CakePHP: Prevent GET form fields in URL

I've got a CakePHP search form that has 'type'=>'get'. Basically, one of the elements in the form is a submit button of type image. When the form is posted, in the URL I always get these x & y coordinates of the image submit button:
http://site.com/controller/action?x=22&y=36&query=hello
Is there any way I can prevent the coordinates from showing up in the URL? The reason is so that someone else could use the same URL to perform the same search, without that unintuitive stuff in the link.
Thanks!
You could use some javascript on the button:
document.getElementById('myImageButton').onclick = function() {
this.form.submit();
return false;
};
Alternatively, in your controller in the beforeFilter function, you could check for the presence of the unwanted variables, strip them out and redirect to the nice URL. This does mean there'll be 2 HTTP requests made though.
Sounds like you are looking to do a Post/Redirect/Get.
Here are two examples of doing this in CakePHP:
Searching on surname
Searching on multiple fields
Advantages of redirecting a POST to a GET request are:
Users don't get the "Do you want to resubmit?" dialog if they refresh
The resulting page/query can be bookmarked
You can utilise CakePHP's built-in SEF routing, so instead of URLs with /search?q=contact you can get /search/contact
Instead of using submit helper function, use button function and set button type to submit.
echo $this->Form->**button**($this->Html->image('header_search_icon.png'), array(**'type'=>'submit'**));

Add and remove form fields in Cakephp

Im looking for a way to have a form in cakephp that the user can add and remove form fields before submitting, After having a look around and asking on the cake IRC the answer seems to be to use Jquery but after hours of looking around i cannot work out how to do it.
The one example i have of this in cake i found at - http://www.mail-archive.com/cake-php#googlegroups.com/msg61061.html but after my best efforts i cannot get this code to work correctly ( i think its calling controllers / models that the doesn't list in the example)
I also found a straight jquery example (http://mohdshaiful.wordpress.com/2007/05/31/form-elements-generation-using-jquery/) which does what i would like my form to do but i cannot work out how to use the cakephp form helper with it to get it working correctly and to get the naming correct. (obviously the $form helper is php so i cant generate anything with that after the browser has loaded).
I an new to cake and have never used jQuery and i am absolutely stumped with how to do this so if anyone has a cakephp example they have working or can point me in the right direction of what i need to complete this it would be very much appreciated.
Thanks in advance
I would take the straight jquery route, personally. I suppose you could have PHP generate the code for jquery to insert (that way you could use the form helper), but it adds complexity without gaining anything.
Since the form helper just generates html, take a look at the html you want generated. Suppose you want something to "add another field", that when clicked, will add another field in the html. Your html to be added will be something like:
<input type="text" name="data[User][field][0]" />
Now, to use jquery to insert it, I'd do something like binding the function add_field to the click event on the link.
$(document).ready( function() {
$("#link_id").click( 'add_field' );
var field_count = 1;
} );
function add_field()
{
var f = $("#div_addfield");
f.append( '<input type="text" name="data[User][field][' + field_count + ']" />' );
field_count++;
}
Of course, if a user leaves this page w/o submitting and returns, they lose their progress, but I think this is about the basics of what you're trying to accomplish.
This was my approach to remove elements:
In the view, I had this:
echo $form->input('extrapicture1uploaddeleted', array('value' => 0));
The logic I followed was that value 0 meant, not deleted yet, and value 1 meant deleted, following a boolean logic.
That was a regular input element but with CSS I used the 'display: none' property because I did not want users to see that in the form. Then what I did was that then users clicked the "Delete" button to remove an input element to upload a picture, there was a confirmation message, and when confirming, the value of the input element hidden with CSS would change from 0 to 1:
$("#deleteextrapicture1").click(
function() {
if (confirm('Do you want to delete this picture?')) {
$('#extrapicture1upload').hide();
// This is for an input element that contains a boolean value where 0 means not deleted, and 1 means deleted.
$('#DealExtrapicture1uploaddeleted').attr('value', '1');
}
// This is used so that the link does not attempt to take users to another URL when clicked.
return false;
}
);
In the controller, the condition $this->data['Deal']['extrapicture1uploaddeleted']!='1' means that extra picture 1 has not been deleted (deleting the upload button with JavaScript). $this->data['Deal']['extrapicture1uploaddeleted']=='1' means that the picture was deleted.
I tried to use an input hidden element and change its value with JavaScript the way I explained above, but I was getting a blackhole error from CakePHP Security. Apparently it was not allowing me to change the value of input elements with JavaScript and then submit the form. But when I used regular input elements (not hidden), I could change their values with JavaScript and submit the form without problems. My approach was to use regular input elements and hide them with CSS, since using input hidden elements was throwing the blackhole error when changing their values with JavaScript and then submitting the form.
Hopefully the way I did it could give some light as a possible approach to remove form fields in CakePHP using JavaScript.

Categories