Test to see if a form exists in a file

Howdy! I know how to test if a file exists but I need to how do you test if a form exists within a file? Thanks.

Yea that would be a lot easier, good idea Tribal_01

Sorry to throw a spanner in the works.

Is it not possible just to store the form in a file and then you can just use file_exists? If it doesn’t exist you could just create the form using php instead.

You can do it in PHP but from browsing posts in this forum I’ve found it to be 2 times harder then what javascript is, here is the basic script for what you need which will need to be modified to house everything you need. There’s a quicker method for making the form which you can also use as well if you think this way is too complex and for you

function checkForm(){
    if (!document.forms['myform']){
        var newForm  = document.createElement('form');
        var element1 = document.createElement('input');
        var element2 = document.createElement('input');
        var element3 = document.createElement('input');
       
        // Set the form element attributes
        newForm.setAttribute('action', ''), newForm.setAttribute('method', 'post'), newForm.setAttribute('name', 'myform');
        element1.setAttribute('type', 'text'), element1.setAttribute('name', 'element_1');
        element2.setAttribute('type', 'radio'), element2.setAttribute('name', 'element_2');
        element3.setAttribute('type', 'checkbox'), element3.setAttribute('name', 'element_3');
       
        // Append the new form elements
        newForm.appendChild(element1), newForm.appendChild(element2), newForm.appendChild(element3);
        
        // Append the new form to the body
        document.getElementsByTagName('body')[0].appendChild(newForm);
    }
}

I need to check if the form exists and if not I need to create it.

Thanks but can’t this be done with php?

This is a job for javascript

HTML

<body onload="checkForm()">

<form name="myform"></form>

JS

function checkForm(){
    if (document.forms['myform']){
        alert('It exists');
    } else {
        alert('It\\'s not there');
    }
}

Thanks for everyone’s suggestions. Here is what I have so far.
The form with error reporting on for debugging.

<?php error_reporting(E_ALL); ?>
<form action="" method="post">
<label for="add_header_text_opt">Add Header Option</label>
<input type="checkbox" name="add_header_text_opt" value="Yes" />
<p><input type="submit" name="submit" value="Create Options" class="button" /></p>
</form>

The code to create the file if it doesn’t exist and to create the form if it doesn’t exist in the file. This creates the file and reports no errors. However, it echos both that the form isn’t found and that it exists. Any suggestions?

<?php
if(isset($_POST["submit"])) : ?>

		<?php if(isset($_POST["add_header_text_opt"]) && $_POST["add_header_text_opt"] = "Yes") : ?>
			<?php $test_file_create = "testFile.php";
			//check if file exists
				if(file_exists($test_file_create)) {
					$check_if_form_exist = file_get_contents($test_file_create);
						if(!strpos($check_if_form_exist, "<form")) {
							echo "Form not found!";
						}
							echo "Form Exists!";
				}
				else {
				if($mk_test_file = fopen($test_file_create, 'w')) {
					if($mk_test_file == false) {
					die("can't open file");
					}
					else {
					$mk_settings_form = '<form name="settings_form action=" method="post">'."\
".'</form>';
					fwrite($mk_test_file, $mk_settings_form);
					}
				}
				fclose($mk_test_file);
				}
				
				 ?>

			<!--<label for="header_text">Enter text to display in header</label><br />
			<input type="text" name="header_text" value="Enter Header Text" />-->
		
		<?php endif; ?>
<?php endif; ?>

What exactly do you mean with ‘form exists within the file’?
That’s there’s HTML code for a form within the file?

In that case you could do something like this:


$code = file_get_contents('/path/to/your/file');
if(strpos($code, '<form')!==false) {
    echo 'File contains a form!';
} else {
    echo 'File does not contain a form...';
}

You could even use a regular expression which might be a better check, but the general flow would be the same.