Php post function with header() - php

I'm new to PHP. I want to send data from one page to another page by post.
if(isset($_REQUEST['submit']))
{
header("location:nextpage.php");
//here i want to send data coming from my text box's by post function
}

You should just target the right page to begin with.
<form action="your-processing-script.php" method="post">
Keep in mind the action is relative, so depending on your file structure, you may want a slash before the file url to prevent confusion.
/your-processing-script.php

Related

Different between actions

I want to know different between <form action="#" method="post"> and <form action="name of file" method="post">
I am always using # but don't know disadvantages.
Can you explain why I should use # or file name?
Thanks
form action = file name
It is used to send a request on the other page(i.e your file name) containing your form fields(inputs) with methods like GET and POST.
example my HTML page is having a form then and my PHP page is having all the backend code. Whatever I need to do with form inputs. I will give the file name of my PHP page in action. the action attribute of the form is used to send the form request to the destination we want to with methods like the POST and GET. If you do not want to send a request to another Page and want it to your default page. You can leave action ='' attribute of the form empty as I did.
An action of # indicates that the form stays on the same page, simply suffixing the URL with a #. A similar use occurs in anchors. Link for example, will stay on the same page.
Thus, the form is submitted to the same page, which then processes the data etc
The content of action allows you to know where you will put the code that will process the request.
If you put the name of a file it, then his file will process the request.
For example: you have your form on the index.php page and you want to put the PHP code of the form in a process.php file. You will put process.php in action (action="process.php").
If you do not put anything it is like sending the content of the request to the same file (index.php).

Trying to load a portion of page with # tag through conroller

I have a page in view that has two parts actually which are accessed through # tags, like login#signin and login#signup. When the page loads for the first time it shows login form without having #signin without a problem.
So signin is not causing a problem as it loads at folder/login. But when I try to put folder/login#signup to load directly signup part it gives an error that there is no view login#signup.php. How to cope with this situation?
$this->load->view('workers/login#signup'); is not working.
When I don't put #signup it loads login form that is weird.
I'll expand more on my initial comments for the cause of this error, and how to fix things.
The cause of the issue
As mentioned throughout the comments, you cannot a view using an anchor point. For example, this does not work:
view('workers/login#signup'); // The #signup should not be here.
The documentation states:
Loading a View
To load a particular view file you will use the following method:
$this->load->view('name');
Where name is the name of your view file.
The name is the file is "name", not "name#signup".
Further down,
The .php file extension does not need to be specified unless you use something other than .php.
This implies, that when you use view('name'), CodeIgniter will, by default, load the file name.php. If you include a #signup in it, then CodeIgniter will not be able to find name#signup.php because that file does not exist.
Correct way to handle things
You mentioned you're using the form validation, so we need to ensure no value is lost during the transition process.
Here's a simplified explanation for how to handle it:
function login() {
// Data to be passed to the view (you may or may not already have this)
// More info: https://codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view
$data = array();
// Validation has failed...
$this->form_validation->run() == FALSE ) {
// Set variable to redirect to #signup upon page load
$data['redirect_to_signup'] = true;
}
// Load view with $data which contains values to be passed to the view
$this->load->view('workers/login', $data);
}
In your workers/login view file, we just need to check if the redirect_to_signup value exists. If it does exist, then we can use some simple JavaScript to scroll down the #signup form:
<?php if (isset($redirect_to_signup) && $redirect_to_signup === true): ?>
<script>
var top = document.getElementById('signup').offsetTop;
window.scrollTo(0, top);
</script>
<?php endif; ?>
Because your validation object is still valid, you can use the built-in CodeIgniter functions to preload your form elements with the set_value() helper functions. For example:
<input type="text" name="email" value="<?php echo set_value('email'); ?>">
That hopefully explains how to achieve what you're after:
Validate user submitted form; and
If there are errors, reload the form with validation messages; and
Scroll down to the #signup form on the page.
One alternative is using redirect('login#signup'), but I would not recommend this method. You would need to save your form values and validation errors to the session to show them on the next page. You also run into the issue that the user might click the refresh button and all values would be lost then.

Sending PHP variable, via jQuery to landing page whilst skipping third party submission script?

My goal is to set a Google Conversion value from a custom field defined in WordPress. The conversion script is located on the landing page, so I need to get my custom field data from my form to the landing page. I can't use GET or POST as the form submission is handled by a third party and no data is returned to the actual landing page.
So I've tried using a PHP session, but this third party is getting in the way of just being able to use PHP, because it's keeping all the data for itself.
This is the approach I'm hoping I can get working:
The validation for the form is done using jQuery Tools.
I then need to submit the variable after validation has been successful via jQuery/AJAX to a separate php file.
Then as the landing page starts to load, I must grab that variable from mentioned PHP file and echo it in the relevant place.
I figured I don't actually need to start a session on the page with the form, as jquery is grabbing the data straight out the input, not any session data. So here's my input with conversion value:
<input type="hidden" id="conv" name="conv" value="90">
Then my form validation:
$("#course-form-modal").validator().submit(function(e) {
// when data is valid
if (!e.isDefaultPrevented()) {
// this grabs the value from my form
var con_val = $("#conv").val();
// and this sends it...
$.post(
"../../usersession.php",
{ data: con_val }
);
}
});
Then I've got the code in usersession.php... where I sent the data:
// As I'm just trying to echo what was sent to this page, via ajax, I shouldn't need to worry about starting/retrieving a SESSION yet... right?
<?php $var_value = $_POST['data']; ?>
<div id="results">
<?php echo $var_value ?>
</div>
// I CAN WORRY ABOUT THIS HALF LATER. RIGHT NOW I JUST WANT TO ECHO MY RESULTS ON USERSESSION.PHP //
Finally, I've got the code on my landing page to retrieve the data from usersession.php:
session_start();
$var_value = $_SESSION['conv'];
echo $var_value;
I'm not entirely sure all this code is right for starters, I'm more of a front end guy...
-EDIT-
Right, I'm pretty sure the code is correct at least now. For some reason it's still not working though. At the moment I'm wondering if WordPress would prevent me writing to usersessions.php from my javascript file (for reference, that file path is set absolutely in my working (not working) example)? I know WordPress will sometimes throw a 404 when you try to access a file directly.
The other potential issue could be with the third party software, vanillasoft. I've a link to their script in the action tag of my form, could that somehow bypass/kill the sending/receiving of data between the form > usersession.php > and then the landing page?
On a side note, if anyone has a great idea on how I can test if usersession.php is receiving the data then please let me know? I did have this code originally, but it returns nothing and if I link straight to the file after a send something (as in just paste the file url in to my browser) it returns a '0'...
if(isset($_POST['conv'])) {
session_start();
$_SESSION['conv'] = $_POST[''conv''];
echo "1";
} else {
echo "0";
}
Set your ID on the input. jQuery is looking for the ID, but you have only set the name.
<input type="hidden" name="conv" value="90">
Should be:
<input type="hidden" name="conv" id="conv" value="90">
EDIT:
Can't believe I didn't catch this earlier. Your problem is in the usersession.php at the following line.
$_SESSION['conv'] = $_POST[''conv''];
You have the POST quoted wrong.
It should be:
$_SESSION['conv'] = $_POST['conv'];
EDIT (re: New js edits)
In you java script your post vars should be formatted thusly:
{ name: "John", time: "2pm" }
So your line should be something like this:
$.post(
'../../usersession.php',
{
conv: $("#conv").val()
},
function(data)
{
alert("Data Loaded: " + $("#conv").val());
}
);

Have html in a string - need to show it as a separate html page using code

I have a script which takes in html from the user as in full page html either from the user or grabs it via curl or from an email. The thing is that I have the html in a string but on the same page I need to show the htmnl in a separate iframe. I don't want to reput any database, curl or imap code in the page referenced by teh iframe at all - is there a way for me to show html passed into a url somehow? like as in a get variable .. the html can be huge here... sorry if it sounds weird.
You can put the grabbed html into a temp file and put the link to that temp file into the src="" of the iframe.
Create them using tempnam(), then create a small script that gets the (preferably obfuscated) filename and simply prints it out if it was really a temp file created by you.
Be careful! if it doesn't check the filename well, you are giving full read access to your server... Put the link to this script in the src of the iframe. You can also create temp files in the public folder of your www server, but I wouldn't want temp/garbage there.
(if i am not misunderstanding your question)
You could put the string in a textarea inside a form and submit the form ..
the receiving page would read the posted data and render it on the page..
I'm not quite sure what you want to do, put you could always post the string as a POST variable, no limitations on how long they can be.
You can encode pieces of HTML with urlencode which is automatically decoded when you retrieve it with $_GET or you can use e.g. base64_encode and base64_decode. The problem is that there are limits to $_GET and $_POST. Both can be set in your configuration settings, but sending large amounts of data via the URL is really not-done because that's not how it should be used.
But if I read your question correctly, you can fetch the HTML at the top of the page, and then load it into an iframe?
if ($_GET['url']) {
$html = file($_GET['url']);
}
if ($_POST['html']) {
$html = $_POST['html'];
}
And then include it:
<html>
..
<iframe ..><php echo $html; ?></iframe>
..
</html>

how to submit a form and show the result in that page

im using a form in php to submit some information into my database
so i used two function to do this
but how to show the result in th same page that has the form
To load the same page you have to assign the variable $_SERVER[PHP_SELF] for the form action field.
<form action='$_SERVER[PHP_SELF]?op=ban' method='post'>
then when the page get load you just check the post variable ,if it contains the appropriate data then print the result with the form.(Normally people using div tag to print the results )
It's as easy as this:
if (isset($_POST['submit']))
{
// do something with your data
}
form();
Forgive me if I am wrong. I think you have copied the code from some where and using it without understanding how forms work.
<form action='index.php?op=ban' method='post'>
The above code says to which page the values should be submitted. As you can see above the values in the form will be submitted to index.php. So the DB operations will(should) happen in index.php and the Thank you message can be shown in index.php.
If you want to show your result in the same page then you will have to submit to the page in which the form resides. But in this case you should have a logic in the page to decide whether the form was submitted or was it loaded first time.
The code snippet in your question does not tell us name of the file the code exists so we wont be able to tell you whether the result will be shown in the same page. Aslo the source code is not complete.
Post a detailed source code and we will be able to help. Hope it helps.
it should be shown on the next request.
because your app should perform an HTTP redirect after POST request.
it can be same page though

Categories