Pass the value of a variable from one page to another [PHP] - php

I would like to pass the value of a variable from one page to another without using a session variable or a cookie, how could I do? Besides this we say that the page must pass the value to another is a page that does not interact with the user as he has only the task of informing him that after a few seconds will be redirected to the next page, to do this I used a Javascript function. I thought then enter the variable value within a field "hidden" But it is not possible to push a button submit do not know how to send data to another page using a form. Could you give me some advice? I am posting my code below:
<?php
if (!mysql_query($sql,$conn)) {
die('Error: ' . mysql_error($conn));
} else {
$PartitaIva = $_POST['PartitaIVA']; echo $PartitaIva;
?>
<form name="passaggiopartitaiva" action="inserimentodati_refaziendale.php" method="post">
<input type="hidden" name="PartitaIva" value="<?= $PartitaIva ?>">
</form>
<p> Modulo inserito a sistema, verrai reindirizzato alla pagina principale </p>
<script>
window.setTimeout ("location.href=('inserimentodati_refaziendale.php')", 1000);
</script>
<?php } ?>

How about setting a timer at load, once your timer runs out, it will submit your form and the user will never know. Try something like this:
$(document).ready(function() {
setTimeout(function() {
$("form").submit();
}, 5000);
});

You can either use some like what CodeGodie posted:
$(document).ready(function() {
setTimeout(function() {
$("form").submit();
}, 5000);
});
or
window.setTimeout ("location.href=('inserimentodati_refaziendale.php?varName=<?= $PartitaIva ?>')", 1000);
And then you could just use $_GET["varName"] on the "inserimentodati_refaziendale" page.
But either way, what is the purpose of doing this? and why do you NOT want to use sessions?

If you're attempting to submit the form after a certain amount of time elapses, you should give the form a name attribute (which you already did, passaggiopartitaiva) and then change:
location.href=('inserimentodati_refaziendale.php')
To:
document.passaggiopartitaiva.submit();
Then the values of your <input> tags will actually be submited, assuming you gave those tags name attributes.
And it would be better to put javascript either in a separate file or in the <head> than to just throw it wherever like you're doing.
If you're using JQuery (which doesn't seem to be the case) then see CodeGodie's answer for a cleaner way to do the Javascript than what you've done.

Related

changing a PHP $SESSION value by clicking on a div

I'm trying to change the value of a PHP session variable when the user clicks on a div element.
There are several div elements on the page and each will change the session variable to a different value.
At the moment I have the following code under the first div as a test but can't get it to change the variable's value.
<div class="workpiece" onclick="<?php $_SESSION['pageRef'] = 1; ?>">
I would greatly appreciate any help as I'm very new to this.
Thanks
You are putting server side code in client side script. You cannot do this, as server side code executes before it is sent to the browser. What you can do, is send an ajax request to a PHP script on click.
<script>
function sendajax() {
$.post('scriptforsession.php', {}, function(response) {
console.log(response);
});
}
</script>
onclick='sendajax();'
Ajax would be one solution but I think what you are trying could also be handled by simply putting the page reference in a GET. Though this will make the page reload. Then in your PHP simple use the $_GET['pageRef']
<div class="workpiece" onclick="window.location='?pageRef=1';">

Submit a Form without Leaving Page

I am working on a survey that will go at the bottom of a FAQ page. My problem is that everytime a form is submitted, it sends you to a different page. I was wondering - is there a way to submit the form and have a little message that replaces the survey that says "Thanks for your feedback" instead of sending the user to another page or refreshing the page?
So far, I have a file that contains the HTML form, CSS, and jQuery and another file that contains the PHP connection to database and insertion of data to the database.
I would appreciate an explanation that is dumbed-down and an example would help since I am relatively new to programming.
An important note: My jQuery is set up to automatically submit if a user answers very helpful/extremely helpful. If not, two more questions appear below with a submit button at the bottom.
More specifically it looks like this:
$(document).ready(function() {
$('.rating').click(function() {
$('.rating').removeClass('selected');
ratingClick(this);
});
});
function ratingClick(that) {
console.log(that.id);
if (that.id == 'rating4' || that.id == 'rating5') {
//$('#questions').fadeOut('slow');
//$('#thankYou').fadeIn('slow');
$('#questions').submit();
} else {
$('#getMore').fadeIn();
$(that).toggleClass('selected');
}
}
$(document).ready(function() {
$('#submit').click(function(){
//$('#questions').fadeOut('slow');
//$('#thankYou').fadeIn('slow');
});
});
What you want is the jquery post function: http://api.jquery.com/jQuery.post/
Make sure your data is JSON.
$("#formdiv").click(function(){
$.post("somepage",{ yourformdata} );
$("#formdiv").replacewith("Thanks for filling out the form!");
});
You can use the replaceWith function to replace the desired content with the thankyou message.
Alex,
from the code you supply, the reason for leaving the page is due to the fact that you don't preventDefault() on the click event. Your page will always reload after that submit unless you take abortive action. No guarantees, but try a quick refactor to:
$(document).ready(function() {
$('#submit').click(function(e){
e.preventDefault();
//$('#questions').fadeOut('slow');
//$('#thankYou').fadeIn('slow');
});
});
This should get you a stage closer. You then just have the ajax logic to define, which should come good with a quick search to match your needs.

jQuery $.get request not working

I have a file upload form, the following javascript fires as soon as the form is submitted:
$("#uploader").submit(function() {
$("#indicator").show();
alert("Submitted");
var refresh = setInterval(function() {
$.get("progress.php?getprogress&randval=" + Math.random(), function(data) {
alert("Got " + data);
$("#indicator .bar div").width(data + "%");
if (data == 100) {
clearTimeout(refresh);
$("#indicator").addClass("done");
}
});
}, 250);
});
I added some alerts to debug, I get the alert("Submitted"), but not the one alerting the data. The php is fine, opening it in a separate window gives the correct values, but the javascript does not get it. Another weird thing is that if I stop the page load, the alert() with the value fires and code is processed.
You are not cancelling the form submission, that means your page will refresh. To cancel the submission you can call event.preventDefault()
$("#uploader").submit(function(e) {
e.preventDefault();
Just moving comments into the answer since your question actually has more to it than what you wrote above.
It is impossible to do two actions on the submit and expect them both to happen. Especially when one is trying to run code as the age is submitting. There are JavaScript libraries that do file uploads, you might want to look into them. BUT the basic idea is submitting the form to a hidden iframe on the page.
<form action="YourSubmitPage.php" method="POST" target="hiddenIframe">
...fileds here...
</form>
<iframe id="hiddenIframe" name="hiddenIframe" style="display:none" />
Ok solved this by using an <iframe>:
<form action="save.php" method="post" target="theiframe">...</form>
<iframe name="theiframe" src="about:blank"></iframe>
Still using javascript, but no e.preventDefault();
I hope your function is inside a $(document).ready() statement

Declaring a Javascript variable into a PHP form

Preapring for a Facebook competition while I have spare time, the Publish function with facebook has a once posted javascript function that you can define.
What I am looking to do is call a function to write a value unto a php form which will then be posted and submitting data into a database. I have tested to the extent that I know the idea is sound just calling a basic alert, I am just not sure how to get from calling the function to writing the value into the form.
This value I need to be able to call on in the page that the data is being posted to, to base an "if function" off, basically if "True/Yes" then I need it to process another php script in addition to the data its posting to the database
What I have now is:
<script type="text/javascript">
function isShared()
{
alert("Yes");
}
</script>
<input class="fieldbox" name='shared' type='hidden' value="value of 'display_alert()'"/>
I know it cannot be an alert, but this is pretty much where my current javascript skills leave me stranded.
<script type="text/javascript">
function isShared()
{
document.getElementById('xshared').value = 'Yes';
}
</script>
<input class="fieldbox" id="xshared" name="shared" type="hidden" value="" />
This will add the value Yes To the hidden field once isShared is called.
Are you looking to have the form automatically posted without the user clicking anything but the share button? The only reason I ask is that it sounds like what you are looking for is AJAX, to post data to the database silently without the need of the user to navigate away from their current page.
I'm still not 100% clear on what you're asking, but are you just looking to emit a server-side PHP variable into client-side JavaScript code? Something like this?:
<?php
// some code, etc.
$myVariable = "foo"; // some value
// more code, etc.
?>
<script type="text/javascript">
var myVariable = '<?php echo $myVariable; ?>';
// more JavaScript code, etc.
</script>
This would emit to the page as:
<script type="text/javascript">
var myVariable = 'foo';
// more JavaScript code, etc.
</script>
Basically anywhere that you need the literal value from the PHP variable, you'd write:
<?php echo $myVariable; ?>
I am not clear how exactly you are aiming to post the form data but you can set the input value using jquery like this:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
$('#input1').val($('#input1').val() + 'more text');
<input id="input1" class="fieldbox" name='shared' type='hidden' value=""/>

ajax call checkbox internet explorer

I'm having this little problem with internet explorer and ajax.
So first I used just php, and everything, worked, but because I don't want to reload the page, I use ajax.
So I have a form with a checkbox. When someone clicks on the checkbox, my ajaex is called and the input is changed in the db. In firefox there is no problem, but It doesn't work in internet explorer.
Here's a part of my code:
<script language="javascript" type="text/javascript">
function changefield($doss, $display){
$.get("update.php",{dossier: $doss, CSQ_DISPLAY:$display});
alert("test");
}
</script>
echo '<form id="'.$r ['BC_DOSSIER'].'" method="get" action="">
<input type="checkbox" name="CSQ_DISPLAY" '.$checked .' onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this.checked)">
</form>';
It seems that in explorer, I only get the alert when the checkbox was checked. (Problem because it first reads the db if it must be checked or not, so you can change it later).
Does someone know where I went wrong?
Thank you very much in advance for the answers.
I would prefer to define it like this, I hate using onclick in my HTML:
edit FIXED (registered to change instead of click)
edit Wrapped in $(document).ready()
edit Added a click event as well
<?php
echo '<input class="ajax_check" id="check_'.htmlspecialchars($r['BC_DOSSIER']).'" type="checkbox" name="CSQ_DISPLAY" '.$checked .' />';
?>
<script type="text/javascript">
$(document).ready(function() {
$('input.ajax_check').click(function() {
this.blur();
});
$('input.ajax_check').change(function() {
var dossierId = this.id.slice(6);
var isChecked = (this.checked) ? 1 : 0; // better to explicitly convert bool to int for HTTP requests
$.get('update.php',{
dossier: dossierId,
CSQ_DISPLAY: isChecked
});
alert('test');
});
});
</script>
This will register that handler to all inputs with the className 'ajax_check', without leaving a function cluttering up the window object, and without messing up your HTML. Try it out and see if it fixes the problem - it may not as it does basically the same thing, but it's a better way of doing it IMHO. If you still have a problem, come back to me and we'll debug it.
Note that using this approach, it is important that the <script> is executed after the DOM is ready, so it should either be defined in the body after the checkbox or (better) wrapped inside $(window).load() or (best) $(document).ready().
In your code fallback function value wont passed for IE.
<input type="checkbox" name="CSQ_DISPLAY" '.$checked .' onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this.checked)">
i.e) in above code onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this.checked)" this.checked will not work for IE, so you can pass as this and get the value in four function, this will work for all the browsers. e.g) like this
<input type="checkbox" name="CSQ_DISPLAY" '.$checked .' onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this)">
and get the checked attribute inside the function.
I hope this will work.

Categories