Cgi C program return value to main HTML and display result - cgi

I am programming a server side script on an Apache machine with cgi. I am using C for the cgi programming. I am a total noob and learning from online examples(I must say except the basics I didn't come across more web sources for detailed learning!).
I am having a simple HTML page where the username(input) is added to a list which is a file I have in my system and then the updated list should be displayed in the SAME PAGE.
I am not able to "print" the results of both the script and http link on the same page so therefore in the code below, you will only see buttons. Please help.
Here is what I have:
Html:
<html>
<head><title>Home</title></head>
<body>
<h1>REGISTER</h1>
<form action= "/cgi-bin/mycgi.cgi" name ="create user" method ="get">
Enter name:<input type="text" name="user">
<br>
<input type="submit" value="add">
</form>
<FORM action="http://localhost:8000/getusers/" method="get">
<P>
<input value="Display Users" type="submit">
</P>
</FORM>
</body>
Here is the cgi Code:
#include<stdio.h>
#include<string.h>
int main(){
char *tmpStr;
char *user;
printf("Content-Type:text/html\n\n");
printf("<html><head><title></title></head><body>");
tmpStr = getenv("QUERY_STRING");
while(tmpStr && *tmpStr != '='){
tmpStr++;
}
user = tmpStr+1,
printf("Adding %s to User Database",user);
//system("wget http://localhost:8000/newuser/");//call script to add user?
printf("</body></html>");
return 0;//return user?
}
Could you please tell me how I can realize these? How can I display the user list without opening a new html site? Also in the above C code, I have to call the link "http://localhost:8000/newuser/" which returns a success or failure value. How can I return it to the parent form?
Thanks.

You could add an iframe to your html:
<iframe id="theiframe" name="theiframe"></iframe>
And then setting the target of your form to the iframe:
<form action= "/cgi-bin/mycgi.cgi" name ="create user" method ="get" target="theiframe">
Anyway, it is not clear to me if the updated list should be displayed when you click on the first or second button.

Related

Cypress doesn't find input field (maybe because inside a form?)

Page source (only iFrame part which contains to form i need to fill)
<iframe title="Form 0" id="hs-form-iframe-0" >
#document
<html>
<body>
<form id="hsForm_405e4c3f-98da-4eb1-bd27-c1886a1f811e">
<div>
<label placeholder="Enter your Vorname">Vorname</span>
<div class="input">
<input name="firstname">
</input>
</div>
</div>
</form>
</body>
</html>
</iframe>
Code i tried:
cy.get('#hs-form-iframe-0').its('0.contentDocument').should('exist')
cy.get('input[name="firstname"]').type( 'Smith') //failes as never found. Is the iFrame the cause of it? Of the form?
TLDR The correct way would be to use .find() on the iframe contentWindow.
cy.get('#hs-form-iframe-0').its('0.contentWindow').should('exist')
.its('body').should('not.be.undefined')
.find('input[name="firstname"]').type( 'Smith')
Example from Working with iframes in Cypress
const getIframeWindow = () => {
return cy.get('iframe[data-cy="the-frame"]')
.its('0.contentWindow').should('exist')
.its('body').should('not.be.undefined')
}
cy.getIframeBody().find('#run-button').should('have.text', 'Try it').click()
There are other potential problems, such as delayed loading of the iframe source. The .should('exist') check on the iframe window does not cover all situations, nor does performing visibility checks on the input.
The cypress-iframe package has a lot more checks built in, so it's a safer way to handle iframes.
You have found the iframe and access its contents but then you search for the input at the root of your DOM instead of the iframe. You can continue the chain of commands by removing the second cy.
cy.get('#hs-form-iframe-0')
.its('0.contentDocument')
.should('exist')
.get('input[name="firstname"]')
.should('be.visible') // always good to check before action
.type( 'Smith')

Handle connection errors in htmxjs

With the power and elegance of HTMXJS and its companion _hyperscriptJS is a matter of few lines to write a code that selects, uploads (POST) multiple files shows a progress bar and then display them:
<form hx-encoding="multipart/form-data"
_="on htmx:xhr:progress(loaded, total) set #bar.value to (loaded/total)*100">
<input type="file" name="fileToUpload[]" multiple
hx-post="upload.php"
hx-target="#image-src"
hx-swap="innerHTML">
<button type="button">Select</button>
<progress id="bar" value="0" max="100"></progress>
</form>
<div id="image-src"></div>
and upload.php:
$countfiles = count($_FILES['fileToUpload']['name']);
for($i=0;$i<$countfiles;$i++){
$filename = $_FILES['fileToUpload']['name'][$i];
move_uploaded_file($_FILES['fileToUpload']['tmp_name'][$i], $filename);
echo '
<div>
<img src="'.$filename.'">
</div>
';
}
but now I would like to add network error handling. I know that HTMX fires
htmx:sendError, but I don't understand how to add it into my code above so that if there's a network error it pops-up a Alert (or swaps/shows the error into a <div>)
You are on the right track, the way to handle this is to hook into the htmx:sendError event.
If you wanted to do this with hyperscript, you could add the following code to your body tag (or any enclosing element of the element issuing the request):
<body _="on htmx:sendError call alert('A network error occured')">
...
</body>

URL address availability - auto verification

I have an input that's in form. I enter the URL adress into this input, click sumbit, and below it shows if this address is available or not. This is done.
Now I want to make this information show up automatically (without clicking submit), after making a change in the input? I would also like the value in the input to not disappear after checking. How can I do that?
I would like it to look more or less like this, but instead of email correctness, URL availability - https://youtu.be/HzJngc-Se9Q
<form action="" method="GET" name="form1" id="form1">
<div id="custom-search-input">
<div class="adress-div">
<input type="text" id="ok" name="domain" maxlenght="30" class="adress" pattern="(.{1,})?([.]{1})?.+[.]{1}.+" placeholder="np. www.page.com" title="Enter URL adress." autocomplete="off" required/>
<br>
<input type="submit" class="button2" value="Check!">
</div>
</div>
</form>
<?php
error_reporting(0);
if(isset($_GET['domain'])){
$domain = $_GET['domain'];
$godaddycheck = 'https://in.godaddy.com/domains/searchresults.aspx?checkAvail=1&tmskey=&domainToCheck='.$domain.'';
$namecomcheck = 'https://www.name.com/domain/search/'.$domain.'';
$registercomcheck = 'http://www.register.co, m/domain/search/wizard.rcmx?searchDomainName='.$domain.'&searchPath=Default&searchTlds=';
if ( gethostbyname($domain) != $domain ) {
echo "<br><br><h1 style='color: #e30000;'><b>$domain</b> not available.</h1>";
}
else {
echo "<br><br><h1 style='color: #00e339;'><b>$domain</b> available.</h1><h2>
</h2>";
}
}
?>
This is not possible doing only with PHP.
You will need to do that with JavaScript. Therefore you need to
listen on an input change for your <input>field
Send the value of the input field with AJAX (https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started) to a PHP script, which then validates the domain
Fetch the response and display it to the user
But there are a lot of tutorials out there, how to do that. For example: https://www.w3schools.com/php/php_ajax_php.asp

WebPagetest not filling in forms, gives "Navigation Error"

I'm new to WebPagetest (https://www.webpagetest.org/) and am having some trouble with login forms. I am using scripting, and can navigate to the page with the form, but it appears as though I am not correctly entering any form values, nor is the form submitting.
Here is a version of my script:
logData 1
navigate https://www.example.com/secure/login
setValue name=username activation_test
setValue name=password password
submitForm id=signInForm
//clickAndWait id=signInBtn
//exec document.getElementsByTagName('html')[0].style.display = 'none';
sleep 5
block google.com/csi
navigate https://www.example.com/?jbhp=true
Of course, my first thought is to make sure that my HTML elements match, but they appear to be fine...
Username:
input tabindex="1" aria-required="true" class="success required" id="username" maxlength="64" name="username" placeholder="Email Address or Username" type="text" value=""
Password:
input tabindex="2" aria-required="true" class="success required" id="password" name="password" placeholder="Password" type="password" value=""
Form:
form action="#" id="signInForm" method="post" class="form formLarge" novalidate="novalidate"
Submit (verified within the form; only used when clickAndWait is uncommented):
button tabindex="3" id="signInBtn" class="ancBtn lrg" data-track-click="sign in : sign in" type="submit"
The result page of the test shows that it navigates to the final location as though not logged in. Also, I see this as part of the results: Step_2
(Error: Navigation Error). Sadly, if there is better documenting as to what specifically caused the navigation error, I have yet to find it.
Finally, if I switch from using "submitForm" to "clickAndWait" (which is commented out in the above example) the only difference in the result is that Step_2 shows a screenshot of the page rather than just an orange block.
IFrames. Stupid IFrames. If you find your way to my question, above, I would bet that your page has IFrames. In my case, my IFrame is coming from the same domain as the browser page -- if this is not the case for your IFrame, I'm not sure if this will work.
To access elements in the IFrame, I had to forget "setValue", "clickAndWait", and so on. I had to go straight to Javascript. Scripted, it looks something like these examples:
exec document.querySelector("iframe[id=stupidIFrameId]").contentDocument.querySelector("input[id=password]").value = 'password'
execAndWait document.querySelector("iframe[id=stupidIFrameId]").contentDocument.querySelector("button[id=signInButton]").click()

Wiring template form actions with Python code

I'm working on a website that has a “Sign up” page which should be callable from anywhere in the site.
I have the following dummy interface and implementation for the “user” product:
Interface:
##
## located in bahmanm/sampleapp/interfaces.py
##
class ISampleAppUser(Interface):
"""
"""
Implementation:
##
## located in bahmanm/sampleapp/implementation/SampleAppUser.py
##
class SampleAppUser:
"""
"""
implements(ISampleAppUser)
# Note that this method is outside of the implementation class.
#
def manage_addSampleAppUser(self, id, title):
# ...
Now, for the moment, let's assume there's a link on the index page which leads to the following template (Sign up template):
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns="http://xml.zope.org/namespaces/tal">
<head><title>Add a new User</title></head>
<body>
<h2>Add a user instance</h2>
<form action="#" method="POST"
tal:attributes="action python:'manage_addSampleAppUser'">
<p>Id: <input type="text" name="id"/></p>
<p>Title: <input type="text" name="title"/></p>
<input type="submit" value="Add"/>
</form>
</body>
</html>
However I haven't been able to find the right value for action property of the form; all I get is a “resource not found”.
Honestly, I believe it's a problem of understanding Zope's mechanisms on my side. I'd really appreciate any hints/clues on where should I go digging for the solution, configure.zcml or the implementation or the template itself. TIA,
You really want to create a view for that; you can call a Product factory like that from a URL too, but it is not recommended.
With a view, you can combine the form and the code to create the new user in one place:
from zope.publisher.browser import BrowserPage
from sampleapp.implementation.SampleAppUser import manage_addSampleAppUser
class NewUserSignup(BrowserPage):
def __call__(self):
# called when the view is being rendered
if 'submit' in self.request:
# form was submitted, handle
self.addUser()
return self.index() # render the template
def addUser(self):
# extract form fields from self.request.form
# validation, error handling, etc.
if someerror:
self.error = 'Error message!'
return
user = manage_addSampleAppUser(self.context, id, title)
# add things to this new user if needed
# all done, redirect to the default view on the new user object
self.request.response.redirect(user.absolute_url())
then register this view with something like:
<browser:page
for="*"
name="signup"
class=".signup.NewUserSignup"
template="signup.pt"
permission="zope.public"
/>
When your new page is registered, the named template is added as a index attribute on your NewUserSignup class, so the __call__ method can invoke it (self.index()) and return the results.
Because you combined the signup handling and the template together, you can now easily incorporate error handling. When someone loads the page for the first time self.request.form will be empty, but as soon as someone hits the submit button, you can detect this and call the addUser method.
That method can either create the user and then redirect away from this page, or set an error message and return, at which point the form is re-rendered.
This makes the action easy to set; you could just leave it empty, or you can set it to the current context URL plus the name of the view. Together, the template then becomes:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns="http://xml.zope.org/namespaces/tal">
<head><title>Add a new User</title></head>
<body>
<h2>Add a user instance</h2>
<div class="errormessage" tal:condition="view/error|nothing" tal:content="view/error">
Conditional error message appears here
</div>
<form action="#" method="POST"
tal:attributes="action string:${context/absolute_url}/##${view/__name__}">
<p>Id: <input type="text" name="id"
tal:attributes="value request/form/id|nothing" /></p>
<p>Title: <input type="text" name="title"
tal:attributes="value request/form/title|nothing" /></p>
<input type="submit" value="Add" name="submit"/>
</form>
</body>
</html>
Note how the form inputs are pre-filled with existing data from the request as well, making it easier for your visitor to correct any errors they may have made.