Hello is it possible if someone could tell me the easiest way to create a dynamic action url on a html form.
We have 2 text box's and we would like to submit the information to a url like the following http://example.com/box1/box2/ but using the values contained within the forms text box's.
I guess this would need to be done via java script or something but i have no idea as to how to go about it.
Any help on this would be appreciated
thanks
It's very simple. All you need to do is to get the values from your script and then use header redirect. e.g.
if(isset($_POST['SUBMIT'] {
$box1 = $_POST['box1'];
$box2 = $_POST['box2'];
header("Location: http://example.com/$box1/$box2/");
}
or you can use PHP curl library if you do not want to do a redirect
if using javascript, guess u could do something like this, php or server RewriteRule (.htaccess) might be what u r looking for.
<script>
//replace the textbox1 and 2 with the id of your text boxes in your form
var box1=document.getElementById('textbox1').value;
var box2=document.getElementById('textbox2').value;
window.location ="http://example.com/"+box1+"/"+box2+"/";
</script>
Might want to put the script into a function and call it with an 'onclick=' event or if u want to call the javascript before the form submits. try using 'onsubmit=' with return=false; at the end of the function.
hope it helps
Related
I have a PHP file which returns me a JSON array based on the GET parameters set. I don't want to reload the page so I made a simple function using jQuery + AJAX to show the result in a div tag.
But there is a problem, because of using AJAX doesn't change the URL and the user won't be able to share the link.
The HTML is called 'calculate.html' and the GET URL would be something like this:
myweb.com/calculate.php?sex&female&age=23&brand=pull%26bear&msg=hello%20stackoverflow
I'd like to know what is the best practice to modify the URL of the HTML when the AJAX call returns the result. And when someone open this URL the HTML reads it and then executes the call with these parameters.
I've modify the .htaccess and this is the url of the html:
myweb.com/calculate/
And with the parameters:
myweb.com/calculate/female/23/pull%26bear/hello%20stackoverflow
Any tip, advice or help would be appreciated, and if you need more information, let me knoe and I'll edit the post.
You can use a # tag in your url
eg http://yoursite.com?param1=23#tempcode
So you can call the ajax code when the url has the #tag tempcode.
In this way you can share your new url
On complete of your AJAX request, you can redirect the page url using
window.location.replace("myweb.com/calculate/female/23/pull%26bear/hello%20stackoverflow");
I want to send some data to a php function with javascript. I have already post a question about this earlier today here, though I didnt get a satisfactory answer, maybe didnt explain myself right.
So I have some variables in javascript( I get it through some API). I would like to send this variables to a function in php. I am using codeigniter so I call the function like("somecontroller/somefunction").
I would like the send the data through post to the function. The function calls a view and reloads the whole html. I am looking for something like sending a form. Only I have the variables in javascript. If there isnt any other way I will dynamicly create a form with jquery and send the data with the .click method on the submit button. But is there a more elegant solution??
I know about ajax, but the problem is that I dont want a asynchronous request I want to just submit the data and then for the new html to load.
The easiest way I can think to solve this would be to send the data through the query string, although you'd have to change the target page to accept GET or REQUEST instead of just POST.
To do this just:
window.location.href = 'somecontroller/somefunction?var1='+var1+'&var2='+var2;
Take a look at this link how to pass arguments to your controller methods in CodeIgniter. Then just redirect user to this link.
If you want to redirect through the Form, set action of form to your controller and in php you can access your form elements through $_GET or $_POST
Since you said you're using jQuery:
$.post("somecontroller/somefunction", {
param1: var1,
param2: var2,
...
}, function(result) { ... });
So basically my question is very simple, I have two buttons, I for page forward, one for page backwards, If one of those is pushed, a javascript function is called inside an onClick Event. Javascript then gets the variables of the page and then redirects to the next page, the only problem is, that I need to pass those variables to PHP in order to put them into the Database. So for that I make a load of cookies to pass the variables.
However, I was wondering if something like this would work :
<form>
<a onClick="nexpage();" onSubmit="phpScript.php"> <img src = "previous button.jpg"/> </a>
</form>
The idea behind this is that I want to store the variables in a PHP script, which will put them in a display:none; <div> and then for javascript to get the variables out. This instead of using cookies.
So is it possible to run a PHP script to get the variables and when the script is finished to get them, Javascript kicks in to redirect to the next page...
The reason I don't test this at this moment, is that my code is 100% complete, I don't want any sudden changes that maybe won't work at all. Yes I know back-up this and that, but I thought just asking here, maybe someone will know the answer!
Sincerly,
Harmen Brinkman
You can also use onClick = "this.form.submit(); return false;".
There is no any event like onSubmit for link, instead form do have onSubmit event.
Normal Way as OP asked.
<form action = "phpScript.php" method = "POST">
you can use document.getElementById("my_form").submit();
#Dipesh Parmar – Good point. You could also do:
window.onload=function() {
document.getElementById('my-form').onsubmit=function() {
// do what you want with the form
// AJAX POST CALL TO PHP PAGE
// Should be triggered on form submit
alert('hi');
// You must return false to prevent the default form behavior
return false;
}
});
Inspiration by Capture a form submit in JavaScript
I have a php script that is a bit of a mess and after a form entry, I need to get an address, and display it on a google map. The html and php is crammed into the same script so I essentially need to call the JavaScript as the PHP is happening. Is there a way to do this?
Thanks,
Alex
You can POST your from to a different frame (or iframe), so your page would not reload. The response of your PHP file which comes back to that frame can contain JavaScript code, which will be executed. Something like:
echo('<script type="text/javascript"> alert("Executed on client side"); </script>');
No, PHP executed by the server and returns the full response to the browser. JavaScript in the page is then executed by the client.
You can't call Javascript functions from PHP. You can set the Javascript to run when the page loads instead.
What you want is something like this:
<script type="text/javascript"></script>
var userAddress = "<?php echo $_POST['address']; ?>";
doSomethingWithAddress(userAddress);
</script>
If that code is on the page which you are POSTing the address to, it would take the address from the user, and write it into a javascript tag. The PHP will get executed first on the server, before building the HTML document. This new document has the variable available to the javascript.
I don't know how you would go about doing that, but this seems like a good place to start looking:
http://code.google.com/intl/en/
Here is the code:
$('#sousmenu a').click (function (){
startSlideshow(<?php echo json_encode(glob("photos-" .$_GET["folder"]. "/*.jpg"));?>);
return false;
});
The question is I like the HREF to change and get caught by PHP, now it doesn't do anything, but writing the ?folder=portraits works.
Here is the page.
**** Simpler *****
Maybe I am not clear, it happens sometimes!
I want the link href to be send to this PHP function,
<?php echo json_encode(glob("photos-" .(i what the href link). "/*.jpg"));?>
so clicking on the link animaux will send animaux to the glob() PHP function and will get all the .jpg files in the photos-animaux folder.
Clicking on the portraits will send the photo-portraits, etc.
If you want to modify the URL and have the added/changed variable picked by PHP interpreter you have to reload your page. Just altering the URL doesn't do anything because JS is executed after PHP processing.
If your site is on http://example.com and you wish a myparam with value test to be passed to PHP you should add something like this in your JS:
document.location = 'http://example.com?myparam=test';
This will reload your page adding a new param which can be accessed in PHP by simply using $_GET['myparam'] variable.
You may also want to consider using AJAX to dynamically changing the contents of your page without having to refresh the whole page, but that's a little bit more complicated.
Look at the source in your browser.
Php is server-side, and that means you need to use ajax or reload whole page to get a response.
There is a nice ajax part of tutorial on jquery website, after reading it you should be able to do what you want: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax