I have a a problem when trying to send data from one page to another via using SESSION.
In page 1 I have a form like:
<form id="myForm" name="myForm" action="" method="post">
<input name="item_1">......</input> // an input for a number
<input name="comment1">......</input> // an input for text
</form>
to avoid refreshing, I use
function submitOnclick()
{
$.post("thisPage.php", $("#myForm").serialize());
redirectToAnotherPage();
}
then I tried to store data via using SESSION
function redirectToAnotherPage(){
<?php $_SESSION['item_1']=$_POST['item_1'] ?>;
<?php $_SESSION['comment1']=$_POST['comment1'] ?>;
location.href='anotherPage.php';
}
but the $POST result is empty, I tried to replace $_POST['item_1'] with a number 1 and it did store the number to item_1, so I assume it is because $_POST['item_1'] has some problems, I don't know why I can't get the form data in one page without submitting / refreshing,
Any help will be appreciated, thanks!
I don't think you can set a PHP session variable like that inside your javascript function. PHP is separate to javascript since PHP is serverside, those values will be read and assigned when the page first pre-processes.
There will be no change, whether the javascript function gets called or not.
Even without AJAX< just a simple PHP form will do. Please see below:
<form id="myForm" name="myForm" action="anotherPage.php" method="post">
<input name="item1">......</input>
<input name="comment1">......</input>
</form>
To submit using javascript, just use something like this on your function:
function submitForm() {
document.myform.submit();
}
the problem is name of your input is not same with your $_POST index
your input: <input name="item1">
your post index: $_POST['item_1']
and also its not right way to use PHP function inside Js, this:
function redirectToAnotherPage(){
<?php $_SESSION['item_1']=$_POST['item_1'] ?>;
<?php $_SESSION['comment1']=$_POST['comment1'] ?>;
location.href='anotherPage.php';
}
you need to set $_SESSION directly in thisPage.php(ajax post url)
EDIT :
dont use serialize(), this way instead:
function submitOnclick()
{
$.post(
"thisPage.php",
{item_1 : $('input[name="item_1"]').val(),
comment1 : $('input[name="comment1"]').val()
}
);
redirectToAnotherPage();
}
good Luck !!!
Related
I want the variables like screen width screen height color depth and pixel depth to be used in a PHP function. Basically what I want to do is when user come in my webpage eg:localhost/try.php I want to know all above mentioned details of client without setting cookies or session properties or get parameters. Yeah and of course without reloading the page. I know php but not AJAX so can anyone help me with that?
if i understood it right, your question is When user comes to your page(e.g try.php)how to pass javascript variable(e.g screen.width screen.height) to PHP section so that you can make it as a PHP variable.
Here is a working solution.
try.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="POST" name="myForm" id="myForm">
<input type="hidden" id="screenWidth" name="screenWidth">
<input type="hidden" id="screenHeight" name="screenHeight">
</form>
<script type="text/javascript">
document.getElementById("screenWidth").value = screen.width;
document.getElementById("screenHeight").value = screen.height;
function autoSubmit ()
{
let form = document.getElementById("myForm");
form.submit();
}
window.onload = autoSubmit;
</script>
</body>
</html>
<?php
if($_SERVER["REQUEST_METHOD"] === 'POST')
{
if(!empty($_POST["screenWidth"]))
{
$screenWidth = $_POST["screenWidth"];
echo $screenWidth; // see if it gets the screen.width
}
if(!empty($_POST["screenHeight"]))
{
$screenHeight = $_POST["screenHeight"];
echo $screenHeight; // see if it gets the screen.height
}
}
?>
It basically manipulate the DOM and change the value of input value to be the screen.width and screen.height and then submit the form automatically when the window is onload.
If the the server received an HTTP POST method input then it checks the name of the POST inputs and save it into each PHP variables.
id doesn't want to put the value in to the document.getElementById("UniLoc").value = val; and it doesn't want to submit the form how to fix this ?
This is how I call the function JS nas some value "asd56"
echo "
<script type="text/javascript">
repostD("'.$JS.'");
</script>";
here is my function inside
<script>
function repostD(val)
{
//document.getElementById("UniLoc").value = val;
document.getElementById('UniLoc').innerHTML = val;
//document.UlRL.UniLoc.value = val;
document.forms["UlRL"].submit();
}
</script>
and here is my form in html
<form name="UlRL" id="UlRL" action="in.php" method="post">
<input type="text" id="UniLoc" name="UniLoc" value="<?php echo $UniLoc;?>" />
Use
document.getElementById('UniLoc').value=val;
If there is nothing else wrong with your full form, it should submit. If you haven't closed your form tag, then close it.
</form>
try doint that in php all instead of in function make an if clause down the code that will execute the action of the posting of the form and at the beggining of the php try this
if($JS!="" )
{
$UniLoc=$JS;
$JS="something that will go throught the other if down at the code";
}
else {
$UniLoc=$_POST["UniLoc"];
}
I cant really tell from your question what is wrong, but looking at your code:
document.getElementById('UniLoc').innerHTML=val;
you are trying to set the value of the input with id 'UniLoc', but you are using the wrong syntax. Instead of .innerHTML, use .value
document.getElementById('UniLoc').value=val;
I have a comment system in which i want to add delete option, for this i have implemented a POST form in each comment which posts comment-id to delete.php, it is working in php, but not in jquery.
i.e in order to delete comment a comment id must be posted to delete.php file which handles deletion of comment from database.
i am trying to fetch that comment-id from input value to post with jquery like this but it gives me the first comment-id value not the selected value.
Jquery
$('form[name=comments]').submit(function(){
var comment_delete = $("input[name=comment-delete]").val();
//$.post('../../delete.php', {value1:comment_delete}, function(data){alert('deleted')});
alert(comment_delete);
return false;
});
repeating form is like this
<form name="comments" action="../../delete.php" method="post">
<input name="comment-delete" type="hidden" value="<?php echo $list['comment-id']; ?>" />
<input value="Delete" type="submit" />
</form>
if i use .each() or .map() it gives me all the comment-id values.
Please see and suggest any possible way to do this.
Thanks.
To find the relevant input, that is the one of the form you submit, you could use this :
$('form[name=comments]').submit(function(){
var comment_delete = $(this).find("input[name=comment-delete]");
BTW, I'm not totally sure of what you do but you might be missing a .val() to get the value of the input.
You have the same name on each hidden input, naturally you get all those inputs as you have not targeted the correct form when doing:
$("input[name=comment-delete]");
"this" whould point to the form inside your submit function. Try this.
$('form[name=comments]').submit(function(){
var comment_delete = $(this).find("input[name=comment-delete]");
//$.post('../../delete.php', {value1:comment_delete}, function(data){alert('deleted')});
alert(comment_delete);
return false;
});
As dystroy said, you are probably missing .val().
var commentId = $(this).find("input[name=comment-delete]").val();
try this
$('form[name=comments]').submit(function(){
var comment_delete = $("input[name=comment-delete]", this);
//$.post('../../delete.php', {value1:comment_delete}, function(data){alert('deleted')});
alert(comment_delete);
return false;
});
this refers to the form being submitted (more generally, to the event source).
$(...) accepts a second parameter, which is then used as a context for the selector. $(selector, context) is equivalent to $(context).find(selector)
I want to send a post from HTML that contains info about a certain form to a controller in code igniter. The I want the controller to process the info and loads a certain page inside a div. Here is my code. I think I'm supposed to use something like .html or something? I'm not quite sure, I dont understand it
The controller
function search_friend(){
//this function gets text from text field and searches for a user and returns all users similar to this dude
// $this->input->post($searchFriendForm);
// $this->input->post($searchFriendText);
$this->load->model('userProfile_m');
$people = $this->userProfile_m->get_user_by_name($this->input->post($searchFriendText));
$this->load->view('addFriendSearchResult',$people);
}
the form in html
<form method="post" action="" name="searchFriendForm" id="add-friend-search">
<input type="text"/ name="searchFriendText">
<input type="button" class="small green button" id="add-friend-button" />
</form>
the jquery function
$("#add-friend-button").click(function(){ //start click
$.post("<?php echo site_url('userProfile/search_friend'); ?>", $("#add-friend-search").serialize());
$("#insert-activity").load("<?php echo base_url().''?>system/application/views/addFriendSearchResult.php");
$("#add-friend-search").slideUp("slow",function(){});
}); //end click
Firstly, in your controller change this line like this (u need to pass the string name of the field here):
$people = $this->userProfile_m->get_user_by_name($this->input->post('searchFriendText'));
Next, change your jQuery to be like this:
$("#add-friend-button").click(function(){ //start click
$.post("<?php echo site_url('userProfile/search_friend'); ?>",
$("#add-friend-search").serialize(),
function(data){
$("#insert-activity").html(data);
});
$("#add-friend-search").slideUp("slow",function(){});
}); //end click
You cant call your view directly, and you don't need to. The post should return the data, which you can write out to your #insert-activity element.
I have several instances where a form is brought into the page in an overlay via ajax. On these forms I have a "math validation" question (I know not the most secure). I am using the following to generate the problem:
<?php
$randomNum = rand(0,9);
$randomNum2 = rand(0,9);
$randomNumTotal = $randomNum + $randomNum2;
?>
the random number is displayed in a text box like so:
<input type="text" name="someName" value="<?= $randomNum ?> + <?= $randomNum2 ?>" readonly>
and validated:
...
SomeName: {
required: true,
equal: <?php echo $randomNumTotal; ?>
}
...
This works fine when the form is on the page, hidden and then displayed with a .click function. I would like to store the files remotely and display them via AJAX. No problem doing that, the problem is when I do this, the numbers generated don't match the value. I've tried putting the PHP $randomeNumTotal function on both the form itself that is brought in via AJAX and on the parent page. Neither works.
So my question is this: Can I store the PHP function in a remote file "random.php", then pass the value to it via .get? If so, how do I make "SomeName" value equal the value from the file?
<script>
$.get( 'random.php', function ( data ) {
$("SomeName").val('random.php')?//I know this isn't right
});
</script>
Thanks!
$.get( 'random.php', function ( data ) {
$("SomeName").val(data);
});
Should work. Alternatively, you could use jQuery's load() if you wanted to load the content in as the HTML of any element.