Jump to content

Need help writing to a file


eldan88
Go to solution Solved by Psycho,

Recommended Posts

Hey Guys. I am trying to write to the file depending on which condition is met. The code works fine on my local machiene but not on my remote server.
 

I have also tried to output any error messages to see if it would output anything, and I don't get anyting on my browser.

Can anyone help me with this issue? Thanks

<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {


isset($_POST['interfax']) ? $option= "interfax" : $option= "";
isset($_POST['metrofax']) ? $option= "metrofax" : $option= "";


switch ($option) {
	case 'interfax':
	$file = "fax.php";
	$fax_client = "interfax";
	if(file_put_contents($file, "<?php ".'$fax_client = "' . $fax_client . '"'." ?>"))
	{ echo "Successful"; } else {
			die("Can't write file");
		}
		break;
	
		


    // By defualt all the orders go to metrofax so by selecting the variable it resets it self 
	case 'metrofax':
	$file = "fax.php";
	$fax_client = "metrofax";
	file_put_contents($file, "<?php ".'$fax_client = "' . NULL . '"'." ?>");
		break;	

			}


		}
?>


<form action="#" method="POST"> 

 <input type="radio" name="interfax" value="interfax">Switch To Interfax<br>
<input type="radio" name="metrofax" value="metrofax">Switch To Metrofax<br>

<input type='submit' name="submit" >
Link to comment
Share on other sites

  • Solution

 

The code works fine on my local machiene but not on my remote server.

 

I find that hard to believe. The radio group isn't even built correctly - you allow the user to select both options. And this makes no sense at all

file_put_contents($file, "<?php ".'$fax_client = "' . NULL . '"'." ?>");

And this ensures that interfax an never be selected since the second line would overwrite the value of $option

 

   isset($_POST['interfax']) ? $option= "interfax" : $option= "";
    isset($_POST['metrofax']) ? $option= "metrofax" : $option= "";
Edited by Psycho
Link to comment
Share on other sites

Phsyco. Thanks for your response. The code I posted was quick and dirty. I know that the radio options can be selected at the same time, but there is only one user that would come on this page.

 

 My question was just why it doesn't output any messaged when the file has been written successfully.

Link to comment
Share on other sites

Many confusing things in your code, so I cleaned It up so I could read it, but I still don't understand what your goal is.

 

Pleae note the changes I made.

<?php
error_reporting(E_ALL | E_STRICT | E_NOTICE);
ini_set('display_errors', '1');
$error_msg = '';
if (isset($_POST['submit'] && $_POST['submit'] == 'Submit'))
{
	if (isset($_POST['fax']))
	{
		$option = $_POST['fax'];
		if ($option <> 'interfax' && $option <> 'metrofax')
		{
			$error_msg = "invalid fax choice";
		}
	}
	else
	{
		$error_msg =  "Fax choice not made";
	}
	//
	switch ($option) 
	{
		case 'interfax':
			$file = "fax.php";
			//$fax_client = "interfax";
			// already have a value for the above
			$faxmsg = "?????";
			if(file_put_contents($file,$faxmsg))
				echo "$option Successful"; 
			else 
				die("$option Can't write file");
			break;
		// By defualt all the orders go to metrofax so by selecting the variable it resets it self 
		case 'metrofax':
			$file = "fax.php";
//			$fax_client = "metrofax";
//			$faxmsg = "<?php ".'$fax_client = "' . NULL . '"'."endphp");
//		You are already in php mode, so no idea what this is supposed to do.
			file_put_contents($file,$faxmsg); 
			if(file_put_contents($file,$faxmsg))
				echo "$option Successful"; 
			else 
				die("$option Can't write file");
			break;	
	}
}
echo $error_msg;
?>
<!--   -->
<!--   -->
<!--  more html preceding this I hope -->
<!--   -->
<!--   -->
<form method="POST"> 
<input type="radio" name="fax" value="interfax">Switch To Interfax<br>
<input type="radio" name="fax" value="metrofax">Switch To Metrofax<br>
<input type='submit' name="submit" value='Submit'>
<!-- -->
<!-- -->
<!--  You need an end form tag -->
<!-- -->
<!-- -->
<!-- -->
</form>
(more html???)
1 - check for a submit button , not a request method

2 - add a value to the submit button

3 - radio buttons must all be grouped under the same name otherwise you will have two values.

4 - you need an end of form as well as much more html

5 - ALWAYS DEVELOP WITH PHP ERROR CHECKING ON !

 

So - what are you trying to write to that fax.php file? Actual php tags?

Link to comment
Share on other sites

@Physco. You where right. The problem was because of how the radio options where structured. (Excuse me I am not to familiar with HTML)

I went ahead and fixed it to the following and it fixed the issue

Thanks!! :happy-04:

<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {

$option = $_POST['faxclient'];

switch ($option) {
    case 'interfax':
    $file = "fax.php";
    $fax_client = "interfax";
    if(file_put_contents($file, "<?php ".'$fax_client = "' . $fax_client . '"'." ?>"))
    { echo "Successful"; } else {
            die("Can't write file");
        }
        break;
    
        


    // By defualt all the orders go to metrofax so by selecting the variable it resets it self
    case 'metrofax':
    $file = "fax.php";
    $fax_client = "metrofax";
    file_put_contents($file, "<?php ".'$fax_client = "' . NULL . '"'." ?>");
        break;    

            }


        }

?>


<form action="#" method="POST">

 <input type="radio" name="faxclient" value="interfax">Switch All Order To Interfax<br>
<input type="radio" name="faxclient" value="metrofax">Switch All Order To Metrofax<br>

<input type='submit' name="submit" >
Link to comment
Share on other sites

@ginerjim. I agree with you about using the error reporting. But you don't have have to take it to far and go with

if (isset($_POST['submit'] && $_POST['submit'] == 'Submit'))

The less code the better and there is no point of checking where the post came from when there is only one form field.

 

I don't think you understand the intent of my code. I don't need a $faxmsg lol

Its just a fall back code if one doesn't work.  But anyway the issues was becuase I had the form field set up. I do appreciate your help :)

Link to comment
Share on other sites

Your submit field had no value clause, so what does the submit have for a value when it comes in? And when checking for the submit you should be checking to be sure that it is the button you expected, regardless of whether there is only one, imho. This is standard practice to ensure you are handling the form correctly (in all situations) and to ensure that what you are getting is what you expect.

Link to comment
Share on other sites

You are right, and I should of removed the value attribute. But as a personal preference I like to go with ($_SERVER['REQUEST_METHOD'] == "POST"). If i ever run into problems with that in the future I will change it.

Link to comment
Share on other sites

You are right, and I should of removed the value attribute. But as a personal preference I like to go with ($_SERVER['REQUEST_METHOD'] == "POST"). If i ever run into problems with that in the future I will change it.

You should NOT have removed the value attribute. You need it! You need to develop good habits and that is one of them. What will you code up when you create a form that is supposed to give the user two actions that can be done? Both (un-valued) submit buttons will have the same default label, but your intent is to have them do different things. Without meaningful labels they are indistinguishable and your php script will not know what to do. As for using request_method that too is worthless since you won't know which button was clicked with that code.

 

My code is very correct. It will only respond to a POST method since there won't be any POST values if the method was not a post. Why check request_method if I'm looking for post vars - THAT would be a waste of code.

Link to comment
Share on other sites

The code is indeed a bit weird. A PHP script which writes a PHP script which sets a variable? Seriously?

 

However, I do think it's valid to check the request method.

 

The point is that the client made a POST request to add or alter data. How they made the request is irrelevant. Maybe they've clicked a submit button, maybe they've hit Enter, maybe the data was submitted with Ajax, maybe they've assembled a raw HTTP message. I don't care.

 

Yes, you can have multiple submit buttons, but that's really a bad idea. If the user just hits Enter, the browser cannot tell which submit button they meant and seems to simply submit the first one. That's hardly acceptable. Of course you could try to block the Enter button with JavaScript, but this forces the user to turn JavaScript on and breaks standard UI behavior. The real solution is to not use multiple submit buttons in the first place. Just make multiple forms.

 

Another problem is that the “value” of a submit button is actually the fancy label on top of it. I find it odd to base my application logic on a string like, say, “Upload your cute kitten videos NOW!”. And what if I want to change that label? Isn't the whole idea of modern programming to separate the logic from the presentation? Sure, you can “solve” this problem by using a button instead of an input element. But then again: Why care about the submit button in the first place?

 

Long story short: It does make sense to check the request method rather than the submit button.

 

Setting the error configuration at runtime is also problematic, because the error can happen before the script runs (a parse error, for example). This should be done in the php.ini or an application-specific .htaccess file or whatever.

 

Also note that E_ALL includes E_NOTICE. And adding E_STRICT is only required in legacy PHP versions before 5.4.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.